zoukankan      html  css  js  c++  java
  • 系统综合实践第三次作业

    (1)完成Docker-compose的安装

    下载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
    

    将可执行权限应用于二进制文件

    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编写

    创建如下目录结构:

    具体文件配置如下:
    dockerfile_nginx

    FROM nginx
    
    MAINTAINER lin
    
    EXPOSE 1998
    

    dockerfile_mysql

    FROM mysql
    MAINTAINER lin
    #设置root密码
    ENV MYSQL_ROOT_PASSWORD 123456
    #设置不可免密登录
    ENV MYSQL_ALLOW_EMPTY_PASSWORD no
    

    dockerfile_php

    FROM php:7.4-fpm
    MAINTAINER lin
    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

    hello man!
    

    index.php

    <?php phpinfo();?>
    

    default.conf

    server {
        listen       1998;        #修改映射端口
        server_name  localhost;
    
        location / {
            root   /web/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           /web/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:1998"                       #修改端口映射
            volumes:
                - ./web:/web/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:/web/php              #挂载工作目录到本机web目录   
    
        mysql:
            image: mysql_image 
            container_name: mysql
            build:
                context: .
                dockerfile: dockerfile_mysql
            ports:
                - "3306:3306"
            volumes:
                - ./mysql:/var/lib/mysql       
    

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

    运行docker-compose

    sudo docker-compose up -d --build
    

    查看容器和镜像

    访问

    (4)服务测试

    以下均修改dc/web/indeks.php
    1.连接测试

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

    2.创建一个名为lin_test数据库

    <?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 lin_test";
    
        // 使用 exec() ,因为没有结果返回
        $conn->exec($sql);
    
        echo "数据库创建成功<br>";
    }
    catch(PDOException $e)
    {
        echo $sql . "<br>" . $e->getMessage();
    }
    $conn = null;
    ?>
    

    浏览器访问

    终端访问

    sudo docker exec -it mysql /bin/bash
    mysql -u root -p
    show databases;
    

    3.创建一个名为user的数据表

    <?php
    $servername = "mysql";
    $username = "root";
    $password = "123456";
    $dbname="lin_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 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
        )";
        // 使用 exec() ,没有结果返回 
        $conn->exec($sql);
        echo "数据表 user 创建成功";
    }
    catch(PDOException $e)
    {
        echo $sql . "<br>" . $e->getMessage();
    }
    $conn = null;
    ?>
    

    浏览器访问

    use lyh_test
    show tables;
    

    4.数据插入

    <?php
    $servername = "mysql";
    $username = "root";
    $password = "123456";
    $dbname="lin_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 user (firstname, lastname, email) 
        VALUES ('Yuhao', 'Lin', 'john@example.com')");
        $conn->exec("INSERT INTO user (firstname, lastname) 
        VALUES ('Jiayi', 'Zhao')");
      $conn->exec("INSERT INTO user (firstname, lastname, email) 
        VALUES ('Li', 'Lin', 'john@example.com')");
        $conn->exec("INSERT INTO user (firstname, lastname) 
        VALUES ('Jiayi', 'J')");
    
        // 提交事务
        $conn->commit();
        echo "新记录插入成功";
    }    
    catch(PDOException $e)
    {
        // 如果执行失败回滚
        $conn->rollback();
        echo $sql . "<br>" . $e->getMessage();
    }
    
    $conn = null;
    ?>
    

    浏览器访问

    select * from user;
    

    5.删除数据

    <?php
    $servername = "mysql";
    $username = "root";
    $password = "123456";
    $dbname="lin_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 user where lastname='Lin'");
    
        // 提交事务
        $conn->commit();
        echo "新记录插入成功";
    }    
    catch(PDOException $e)
    {
        // 如果执行失败回滚
        $conn->rollback();
        echo $sql . "<br>" . $e->getMessage();
    }
    
    $conn = null;
    ?>
    

    浏览器访问

    select * from user;
    

    (6)

    主要问题:
    就是查看目录结构时候要先安装一下
    感谢:
    自己查资料了解了dc这个工具作用,但是还是不会用,然后看博客里面有些人已经做完了,参考他们的步骤,还是很顺利的完成了,非常感谢先做完的同学们,提供了很大帮助
    时间:

    作业名称 耗时(小时)
    Docker-compose的安装 0.5
    Dockerfile编写以及使用Compose实现多容器运行机制 3
    服务测试 3
    博客编写 1.5
    总计 8
  • 相关阅读:
    Python之格式化unix时间戳
    Python简单的验证码生成
    Python字符串常用的一些东西
    PHP explode()函数
    PHP函数number_format()
    PHP简单的计算位数的函数
    python之列表推导式
    python之把列表当做队列使用
    python之列表操作的几个函数
    python之map函数
  • 原文地址:https://www.cnblogs.com/hurricane1/p/12828041.html
Copyright © 2011-2022 走看看