第3次实践作业
第3次实践作业
1、安装compose
从 Github 上下载它的二进制包来使用,最新发行的版本地址:https://github.com/docker/compose/releases。
运行以下命令以下载 Docker Compose 的当前稳定版本:
sudo curl -L "https://github.com/docker/compose/releases/download/1.25.5/docker-compose-\((uname -s)-\)(uname -m)" -o /usr/local/bin/docker-compose
要安装其他版本的 Compose,可替换 1.25.5。
将可执行权限应用于二进制文件:
sudo chmod +x /usr/local/bin/docker-compose
创建软链:
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
测试是否安装成功:
docker-compose --version
2、Dockerfile编写
(1)、拉取所需要镜像:
docker pull php:7.4-fm
(2)、Dockerfile编写
mysql:
FROM mysql:5.7
php:
FROM php:5.6.37-fpm-alpine3.7
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories \
&& apk update\
&& apk add --no-cache libmcrypt-dev freetype-dev libjpeg-turbo-dev \
git \
# libfreetype6-dev \
# libjpeg62-turbo-dev \
libpng-dev \
&& docker-php-ext-install mcrypt mysqli pdo pdo_mysql mbstring bcmath zip opcache\
&& docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ --with-png-dir=/usr/include/ \
&& docker-php-ext-install -j$(nproc) gd
nginx:
FROM nginx:latest
(3)、使用Compose实现多容器运行机制
docker-compose.yml:
version: "3"
services:
web_server:
build: ./nginx
container_name: nginx
networks:
- new
#links:
# - php:php72
depends_on:
- php
ports:
- "82:80"
- "443:443"
volumes:
- ./nginx/www/html:/var/www/html
- ./nginx/conf.d:/etc/nginx/conf.d
db_server:
build: ./mysql
container_name: mysql57
networks:
- new
environment:
MYSQL_ROOT_PASSWORD: 123456
ports:
- "3308:3306"
volumes:
- ./mysql/data:/var/lib/mysql
php:
build: ./phpfpm
container_name: php72
networks:
- new
#links:
# - db_server:mysql57
depends_on:
- db_server
ports:
- "9000:9000"
volumes:
- ./nginx/www/html:/var/www/html
- ./nginx/conf.d:/etc/nginx/conf.d
networks:
new:
执行docker-compose:
docker-compose up -d
(4)、服务测试
compose-lnmp/nginx/www/html/index.html:
hello! this is index
compose-lnmp/nginx/www/html/pdo.php:
<?php
//$servername = "localhost:3308";
//$servername = "127.0.0.1:3308";
//$servername = "172.19.0.2:3306";
$servername = "mysql57:3306";
$username = "root";
$password = "123456";
echo "begin connect by pdo ......!\n";
try {
$conn = new PDO("mysql:host=$servername;", $username, $password);
echo "连接成功";
}
catch(PDOException $e)
{
echo $e->getMessage();
}
$conn = null;
echo "\nclose now!\n";
?>
测试:
127.0.0.1:82/pdo.php
compose-lnmp/nginx/www/html/mysqli.php:
<?php
$servername = "mysql57:3306";
$username = "root";
$password = "123456";
$dbname="myDB";
// 创建连接
try {
$conn = new PDO("mysql:host=$servername", $username, $password);
echo "数据库连接成功<br>";
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "CREATE DATABASE ". $dbname;
$conn->exec($sql);
echo "数据库创建成功<br>";
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); #建立新连接并选择数据库
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "CREATE TABLE S(
sno VARCHAR(20) PRIMARY KEY,
sname VARCHAR(20) NOT NULL,
sex VARCHAR(2) NOT NULL,
CHECK(`sex`='男' OR `sex`='女')
)";
$conn->exec($sql);
echo "表格创建成功<br>";
}
catch(PDOException $e)
{
echo $e->getMessage();
}
$conn = null;
?>
测试:
127.0.0.1:82/mysqli.php
插入数据:
<?php
$servername = "mysql57:3306";
$username = "root";
$password = "123456";
$dbname="myDB";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// 开始事务
$conn->beginTransaction();
// SQL 语句
$conn->exec("INSERT INTO S(sno,sname,sex) VALUES
('031700000','小明','M'),
('031700001','小红','G'),
('031700002','小张','M')");
// 提交事务
$conn->commit();
echo "数据插入成功!";
}
catch(PDOException $e)
{
// 如果执行失败回滚
$conn->rollback();
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
?>
(5)、选做:
新建一个文件夹phpmyadmin,放入Dockerfile
Dockerfile:
#基础镜像
FROM phpmyadmin/phpmyadmin
#作者信息
MAINTAINER SilentSamSara
#声明暴露的端口
EXPOSE 80
在docker-compose.yml最后的network前插入下列代码
phpmyadmin:
image: myphpadmin_image
container_name: myphpmyadmin
build: ./phpmyadmin
networks:
- new
depends_on:
- web_server
ports:
- "8080:80" # phpmyadmin默认监听80
environment:
PMA_HOST: db_server
执行:
sudo docker-compose up -d --build
浏览器输入以下地址: