第3次实践作业
(1)完成Docker-compose的安装
-
由于官方提供的下载方式速度过慢,从网上找到了换源的方案
-
进入root用户
sudo -i
- 进行Docker-compose安装
curl -L "https://get.daocloud.io/docker/compose/releases/download/1.25.4/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
- 查看安装是否成功
docker-compose --version
(2)Dockerfile编写
- 选择LNMP(Linux+Nginx+Mysql+PHP)完成实验
- 目录结构
- dockerfile_nginx
#基础镜像
FROM nginx
#作者信息
MAINTAINER by ruifeng1<460886524@qq.com>
#声明暴露的端口
EXPOSE 2020
- dockerfile_mysql
#基础镜像
FROM mysql
#作者信息
MAINTAINER by ruifeng1<460886524@qq.com>
#设置root密码
ENV MYSQL_ROOT_PASSWORD 123456
#设置不可免密登录
ENV MYSQL_ALLOW_EMPTY_PASSWORD no
- dockerfile_php
#基础镜像
FROM php:7.4-fpm
#作者信息
MAINTAINER by ruifeng1<460886524@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
- default.conf
server {
listen 2020; #修改映射端口
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /home/docker_compose/html; #修改工作目录
index index.html index.htm index.php;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root /home/docker_compose/php; #修改工作目录
# fastcgi_pass lrfphp:9000; #修改为容器名
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
- index.html
<!DOCTYPE html>
<html>
<head>
<title>MyNginx</title>
</head>
<body>
<h1>hello world</h1>
<h1>welcome to 031702533</h1>
</body>
</html>
- index.php
<?php phpinfo();?>
(3)使用Compose实现多容器运行机制
- 运行docker-compose
- 查看容器和镜像
- 访问网站
(4)服务测试
- 连接测试
- 修改index.php
<?php
$servername = "lzssql";
$username = "root";
$password = "123456";
try {
$conn = new PDO("mysql:host=$servername;", $username, $password);
echo "连接成功";
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
- 创建数据库
- 修改index.php
<?php
$servername = "lzssql";
$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 test";
// 使用 exec() ,因为没有结果返回
$conn->exec($sql);
echo "数据库创建成功<br>";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
?>
- 查看数据库信息
docker exec -it lzssql /bin/bash
mysql -u root -p
show databases;
- 创建数据表
- 修改index.php
<?php
$servername = "lzssql";
$username = "root";
$password = "123456";
$dbname="test";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// 设置 PDO 错误模式,用于抛出异常
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// 使用 sql 创建数据表
$sql = "CREATE TABLE test (
id bigint(20) NOT NULL,
name varchar(255) NOT NULL,
email varchar(255) NOT NULL
)";
// 使用 exec() ,没有结果返回
$conn->exec($sql);
echo "数据表创建成功";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
?>
查看数据表
use test
show tables;
- 插入数据
修改index.php
<?php
$servername = "lzssql";
$username = "root";
$password = "123456";
$dbname="test";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// 开始事务
$conn->beginTransaction();
// SQL 语句
$conn->exec("INSERT INTO test (`id`, `name`,`email`)
VALUES(100,'john','1345311153@qq.com');");
$conn->exec("INSERT INTO test (`id`, `name`,`email`)
VALUES(31702115,'lzs','1345311153@qq.com');");
// 提交事务
$conn->commit();
echo "新记录插入成功";
}
catch(PDOException $e)
{
// 如果执行失败回滚
$conn->rollback();
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
?>
- 删除数据
- 修改index.php
<?php
$servername = "lzssql";
$username = "root";
$password = "123456";
$dbname="test";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// 开始事务
$conn->beginTransaction();
// SQL 语句
$conn->exec("delete from test where id=100");
// 提交事务
$conn->commit();
echo "记录删除成功";
}
catch(PDOException $e)
{
// 如果执行失败回滚
$conn->rollback();
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
?>
(5)总结
- 遇到的问题
1.使用官方渠道下载docker-compose速度过慢
2.使用网上的换源方案时,发现sudo指令权限依然不够,于是直接使用sudo -i登录root用户,执行成功
3.运行docker-compose时,php构建失败,原因是执行apt-get update时失败
4.运行docker-compose时,进行访问网站操作,出现了连接被重置的问题
总用时:12小时