2020系统综合实践 第3次实践作业
安装Docker Compose
通过pip安装
$ sudo pip install -U docker-compose
//为文件添加可执行权限
$ sudo chmod +x /usr/local/bin/docker-compose
//创建软链
$ sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
//查看是否安装成功
$ docker-compose --version
Dockerfile编写
mysql
#基础镜像
FROM mysql
#作者信息
MAINTAINER <ljy, 1730166565@qq.com>
#设置root密码
ENV MYSQL_ROOT_PASSWORD 123456
#设置不可免密登录
ENV MYSQL_ALLOW_EMPTY_PASSWORD no
php
#基础镜像
FROM php:7.4-fpm
#作者信息
MAINTAINER <ljy, 1730166565@qq.com>
FROM php:7.4-fpm
RUN apt-get update && apt-get install -y \
libfreetype6-dev \
libjpeg62-turbo-dev \
libpng-dev \
&& docker-php-ext-install pdo_mysql \
&& docker-php-ext-configure gd --with-freetype --with-jpeg \
&& docker-php-ext-install -j$(nproc) gd
nginx
#基础镜像
FROM nginx
#作者信息
MAINTAINER <ljy, 1730166565@qq.com>
#声明暴露的端口
EXPOSE 2020
配置文件
server {
listen 2020; #修改映射端口
server_name localhost;
location / {
root /my_html; #修改工作目录
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
root /my_php; #修改工作目录
fastcgi_pass myphp:9000; #修改为容器名
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
使用Compose实现多容器运行机制
docker-compose.yml
version: "3"
services:
nginx:
image: mynginx_image #指定镜像名
container_name: mynginx #指定容器名
build:
context: .
dockerfile: Dockerfile_Nginx #指定dockerfile文件
ports:
- "80:2020" #修改端口映射
volumes:
- ./web:/my_html
- ./default.conf:/etc/nginx/conf.d/default.conf #配置文件
php:
image: myphp_image
container_name: myphp
build:
context: .
dockerfile: Dockerfile_Php
environment:
MYSQL_PASSWORD: 123456
volumes:
- ./web:/my_php
mysql:
image: mysql_image
container_name: mysql
build:
context: .
dockerfile: Dockerfile_Mysql
ports:
- "3306:3306"
volumes:
- ./mysql_data:/var/lib/mysql
运行docker-compose
$ sudo docker-compose up -d --build
- 查看容器和镜像
- 访问 localhost/index.html
服务测试
-
连接测试 修改index.php
<?php #需要根据自己的实际情况配置 $servername = "mysql"; $username = "root"; $password = "123456"; try { $conn = new PDO("mysql:host=$servername", $username, $password); echo "连接成功!!!"; } catch(PDOException $e){ echo $e->getMessage(); }
-
创建一个Student数据库
修改index.php文件
<?php $servername = "mysql"; $username = "root"; $password = "123456"; try { $conn = new PDO("mysql:host=$servername", $username, $password); // 设置 PDO 错误模式为异常 $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $sql = "CREATE DATABASE Student"; // 使用 exec() ,因为没有结果返回 $conn->exec($sql); echo "数据库创建成功<br>"; } catch (PDOException $e) { echo $sql . "<br>" . $e->getMessage(); } $conn = null; ?>
-
创建一个student表
<?php $servername = "mysql"; $username = "root"; $password = "123456"; $dbname="Student"; try { $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); #选择数据库 $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // 使用 sql 创建数据表 $sql = "CREATE TABLE student( id int(4) not null primary key, name varchar(16) not null ); "; // 使用 exec() ,没有结果返回 $conn->exec($sql); echo "数据表 student 创建成功"; } catch(PDOException $e) { echo $sql . "<br>" . $e->getMessage(); } $conn = null; ?>
-
插入数据
<?php $servername = "mysql"; $username = "root"; $password = "123456"; $dbname="Student"; try { $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); // 设置 PDO 错误模式,用于抛出异常 $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); //开始事务 $conn->beginTransaction(); $conn->exec("INSERT INTO student (id,name) VALUES ('0','Tom')"); $conn->exec("INSERT INTO student (id,name) VALUES ('1','Jack')"); $conn->exec("INSERT INTO student (id,name) VALUES ('2','Pony')"); // 提交事务 $conn->commit(); echo "插入数据成功!"; } catch(PDOException $e) { $conn->rollback(); echo $sql . "<br>" . $e->getMessage(); } $conn = null; ?>
-
删除数据
<?php $servername = "mysql"; $username = "root"; $password = "123456"; $dbname="Student"; try { $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); // 设置 PDO 错误模式,用于抛出异常 $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); //开始事务 $conn->beginTransaction(); $conn->exec("DELETE FROM student WHERE id='0'"); // 提交事务 $conn->commit(); echo "插入数据成功!"; } catch(PDOException $e) { $conn->rollback(); echo $sql . "<br>" . $e->getMessage(); } $conn = null; ?>
选做
Dockerfile
#基础镜像
FROM phpmyadmin/phpmyadmin
#作者信息
MAINTAINER <ljy, 1730166565@qq.com>
#声明暴露的端口
EXPOSE 8080
docker-compose.yml添加相关配置
phpmyadmin:
image: phpmyadmin_image
container_name: myphpmyadmin
build:
context: .
dockerfile: Dokcerfile_Phpmyadmin
ports:
- "8080:80"
environment:
PMA_HOST: mysql
运行docker-compose
//先停止之前运行的docker-compose
$ docker-compose down
$ docker-compose up -d --build
数据库测试
- 访问localhos:8080
- 登录并查看数据库
- 插入、删除数据
实验小结
前面参考同学的博客都比较顺利,选做重新运行docker-compose build的时候老是报错[14772] Cannot open self /usr/local/bin/docker-compose or archive /usr/local/bin/docker-compose.pkg
,可能是我用pip安装的缘故?后面换了一种方法重新装了docker-compose解决了,有可视化界面还是很友好的~~。总的来说比较顺利。这次作业大概花了7个小时的样子,大部分时间都在看博客和相关资料。