系统综合实践(三)-Docker compose

第3次实践作业

(1)完成Docker-compose的安装

参考资料:Install Docker Compose

-curl -L 

-https://get.daocloud.io/docker/compose/releases/download/1.25.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose

-sudo chmod +x /usr/local/bin/docker-compose

验收

(2)Dockerfile编写

  • dockerfile_nginx

    #基础镜像
    FROM nginx
    #作者信息
    MAINTAINER joey<214253089@qq.com>
    #声明暴露的端口
    EXPOSE 2020
    
  • dockerfile_mysql

    #基础镜像
    FROM mysql
    #作者信息
    MAINTAINER joey<214253089@qq.com>
    #设置root密码
    ENV MYSQL_ROOT_PASSWORD 123456
    #设置不可免密登录
    ENV MYSQL_ALLOW_EMPTY_PASSWORD no
    
  • dockerfile_php

    #基础镜像
    FROM php:7.4-fpm
    #作者信息
    MAINTAINER joey<214253089@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
    
  • index.html
    it's a godanm html!!!

  • index.php
    <?php phpinfo();?>

  • default.conf

    server {
        listen       2020;        #修改映射端口
        server_name  localhost;
    
        location / {
            root   /joey/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           /joey/php;          #修改工作目录
            fastcgi_pass   myphp:9000;        #修改为容器名
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name; 
            include        fastcgi_params;
        }
    }
    
  • docker-compose.yml

    version: "3"
    services:
        nginx:
            image: mynginx_image                #指定镜像名
            container_name: mynginx             #指定容器名
            build:
                context: .
                dockerfile: dockerfile_nginx      #指定dockerfile文件
            ports:
                - "80:2020"                       #修改端口映射
            volumes:
                - ./web:/joey/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:/joey/php                
    
        mysql:
            image: mysql_image 
            container_name: mysql
            build:
                context: .
                dockerfile: dockerfile_mysql
            ports:
                - "3306:3306"
            volumes:
                - ./mysql_data:/var/lib/mysql       
    

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

运行docker-compose

sudo docker-compose up -d --build

查看

sudo docker ps -a

访问

(4)服务测试

  • 测试数据库连接
<?php
#需要根据自己的实际情况配置
$servername = "mysql";    
$username = "root";
$password = "123456";     

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

img

  • 数据库新建

下面的代码创建一个名为test的数据库

<?php
$servername = "mysql";
$username = "root";
$password = "123456";

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

    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sql = "CREATE DATABASE test";

    $conn->exec($sql);

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

  • 表新建
<?php
$servername = "mysql";
$username = "root";
$password = "123456";
$dbname="test";    

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sql = "CREATE TABLE user (
    id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
    firstname VARCHAR(30) NOT NULL,
    lastname VARCHAR(30) NOT NULL,
    email VARCHAR(50),
    reg_date TIMESTAMP
    )";
    $conn->exec($sql);
    echo "数据表 user 创建成功";
}
catch(PDOException $e)
{
    echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
?>
  • 表记录的插入修改
<?php
$servername = "mysql";
$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();
    $conn->exec("INSERT INTO user (firstname, lastname, email) 
    VALUES ('ge', 'wuyin', 'a@wuyin.com')");
    $conn->commit();
    echo "新记录插入成功";
}    
catch(PDOException $e)
{
    // 如果执行失败回滚
    $conn->rollback();
    echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
?>
  • 删除是否成功

$conn->exec("DELETE FROM user where lastname='wuyin'");

(5)选做

增加一个phpmyadmin容器,实现web端的数据库管理。

  • 在docker-compose.yml文件中增加

            phpmyadmin:
                image: myphpmyadmin_image
                container_name: myphpmyadmin
                build: 
                    context: .
                    dockerfile: dockerfile_phpmyadmin
                ports: 
                    - "8080:80" 
                environment:
                    PMA_HOST: mysql                     #指定mysql服务所在的host
    
  • dockerfile_phpmyadmin

    #基础镜像
    FROM phpmyadmin/phpmyadmin            
    #作者信息
    MAINTAINER joey<214253089@qq.com>
    #声明暴露的端口
    EXPOSE 8080
    

(6)实验总结

花费时间:3h观看各类docker_compose视频,3h查阅各类文档,1.5h实验(装了30min的compose),1h写blog,半小时来思考为什么挺简单的实验花费了这么多时间,共计8h。

得结论:对web服务端知识不熟悉,盲人摸象,所以以后类似问题应该先把大象装冰箱(雾),自顶向下的学习。

posted @ 2020-05-07 12:58  跟谁脑子好使似的  阅读(274)  评论(0编辑  收藏  举报