2020系统综合实践 第3次实践作业

完成Docker-compose的安装

  • 换源
curl -L https://get.daocloud.io/docker/compose/releases/download/1.25.5/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
  • 修改目录权限,否则后续docker-compose up命令会报错
sudo chmod +x /usr/local/bin/docker-compose
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
docker-compose --version


Dockerfile编写

创建如下各个文件

html

#index.html
welcome to cthnginx 
#index.php
<?php phpinfo();?>

mysql

#Dockerfile
#基础镜像
FROM mysql:5.7

#维护者信息
MAINTAINER cth

nginx

#Dockerfile
# Base images 基础镜像
FROM nginx:latest

#MAINTAINER 维护者信息
MAINTAINER cth
#default.conf
server {
        listen       80;
        server_name  localhost;
    
        location / {
            root   /var/www/html;
            index  index.html index.htm index.php; # 增加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   /var/www/html;
        }
        location ~ \.php$ {
            root           /var/www/html; # 代码目录
            fastcgi_pass   phpfpm:9000;    # 修改为phpfpm容器
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name; # 修改为$document_root
            include        fastcgi_params;
        }
    }

phpfpm

#Dockerfile
FROM php:5.6-fpm

RUN apt-get update && apt-get install -y \
libfreetype6-dev \
libjpeg62-turbo-dev \
libpng12*-dev \
vim \
&& docker-php-ext-install pdo_mysql \
&& docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
&& docker-php-ext-install gd \

使用Compose实现多容器运行机制

docker-compose.yml

version: "3"
services:
 nginx:
   build: ./nginx
   ports:
     - "80:80"
   links:
     - "phpfpm"
   volumes:
     - ./html/:/var/www/html/
     - ./nginx/conf/default.conf:/etc/nginx/conf.d/default.conf
 phpfpm:
   build: ./phpfpm
   ports:
     - "9000:9000"
   volumes:
     - ./html/:/var/www/html/
   links:
     - "mysql"
 mysql:
   build: ./mysql
   ports:
     - "3306:3306"
   volumes:
     - ./mysql/data/:/var/lib/mysql/
   environment:
     MYSQL_ROOT_PASSWORD : 123456
  • 在compose文件夹打开终端
docker-compose up -d
  • 构建成功

服务测试

测试访问index.html和index.php,


  • 连接mysql
<?php
$servername = "localhost";
$username = "username";
$password = "password";
 
try {
    $conn = new PDO("mysql:host=$servername;", $username, $password);
    echo "连接成功"; 
}
catch(PDOException $e)
{
    echo $e->getMessage();
}
?>

  • 创建数据库
<?php
$servername = "localhost";
$username = "username";
$password = "password";

try {
    $conn = new PDO("mysql:host=$servername", $username, $password);

    // 设置 PDO 错误模式为异常
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sql = "CREATE DATABASE myDBPDO";

    // 使用 exec() ,因为没有结果返回
    $conn->exec($sql);

    echo "数据库创建成功<br>";
}
catch(PDOException $e)
{
    echo $sql . "<br>" . $e->getMessage();
}

$conn = null;
?>

  • 用终端进入mysql容器,查看数据库是否创建成功
docker  exec -it compose_mysql_1 /bin/bash
mysql -u root -p
show databases;

  • 在数据库内建表
<?php
$servername = "192.168.10.139";
$username = "root";
$password = "123456";
$dbname = "myDBPDO"; 
 
try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    // 设置 PDO 错误模式,用于抛出异常
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
 
    // 使用 sql 创建数据表
    $sql = "CREATE TABLE palyer (
    id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
    firstname VARCHAR(30) NOT NULL,
    lastname VARCHAR(30) NOT NULL,
    email VARCHAR(50),
    reg_date TIMESTAMP
    )";
 
    // 使用 exec() ,没有结果返回 
    $conn->exec($sql);
    echo "数据表 player 创建成功";
}
catch(PDOException $e)
{
    echo $sql . "<br>" . $e->getMessage();
}
 
$conn = null;
?>

use myDBPDO
show tables;

  • 插入数据

  • 验证

  • 更新数据

  • 验证

  • 删除数据

  • 验证

增加phpmyadmin容器(选做)

  • pull phpmyadmin
docker pull phpmyadmin/phpmyadmin
  • 在文件夹compose中创建以下文件

Dockerfile

#Dockerfile
FROM phpmyadmin/phpmyadmin:latest

在docker-compose.yml文件末端加上

 phpmyadmin:
    build: ./phpmyadmin
    container_name: "phpmyadmin"
    ports:
     - "9090:80"
    links:
     - "mysql"
    environment:
     - PMA_ARBITRARY=1  
  • 重构docker-compose

  • 在web端使用phpmyadmin,用自己数据库的账号密码进入

  • 插入数据

  • 使用phpmyadmin查询

总结

  • docker-compose官方安装速度太慢,加速器用后就好
  • 编写docker_phpmyadmin运行失效,换名字就行
  • build过程中apt-get update更新很慢,试了几个网上常见的加速方式也不见效,无奈挂机慢慢下
  • 跟着大佬的步骤加上自己的一些理解,学起来会比较顺
  • 用时:加起来差不多一天
posted @ 2020-05-08 19:57  -CTH  阅读(226)  评论(0编辑  收藏  举报