• 第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 ......!
    ";
    
    try {
        $conn = new PDO("mysql:host=$servername;", $username, $password);
        echo "连接成功"; 
    }
    catch(PDOException $e)
    {
        echo $e->getMessage();
    }
    $conn = null;
    echo "
    close now!
    ";
    ?>
    

    测试:

    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

    浏览器输入以下地址:

    http://127.0.0.1:8080/index.php

    (6)、实验总结

    • 可执行文件
    • 遇到的问题
      • 开始使用docker-compose是不知道Dockerfile要放在容器名的文件夹底下
      • 端口映射有点问题,感觉有点乱
    • 解决方法
      • 参考了一个例子.链接
    • 完成记录所花的时间
      • 集中在一个下午和晚上,还有一些零碎的时间,大概8个小时
  • 相关阅读:
    结对项目:四则运算
    Word Count(C语言)
    自我介绍+软工5问
    如何在博客园中使用markdown
    如何设计一门语言(九)——类型
    如何设计一门语言(八)——异步编程和CPS变换
    如何设计一门语言(七)——闭包、lambda和interface
    时隔多年我又再一次体验了一把跟大神聊天的感觉
    20199107 2019-2020-2 《网络攻防实践》第4周作业
    20199107 2019-2020-2 《网络攻防实践》第3周作业
  • 原文地址:https://www.cnblogs.com/SilentSamsara/p/12853216.html
Copyright © 2020-2023  润新知