第3次实践作业

第三次实践作业

(1)完成Docker-compose的安装

  • 下载最新的docker-compose版本
curl -L "https://github.com/docker/compose/releases/download/1.25.5/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

  • chmod +x /usr/local/bin/docker-compose给文件添加执行权限,ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose创建软连接至一个PATH目录下,一般为/usr/bin
  • docker-compose --version 出现版本号表示安装成功

(2)Dockerfile编写

选择的是LNMP

  • 在项目文件夹中创建文件
touch docker-compose.yml dockerfile_mysql dockerfile_nginx dockerfile_php default.conf && mkdir mysql_data web && tree

  • 拉取所需的镜像(mysql,nginx,php)

    • mysql: docker pull mysql

    • nginx: docker pull nginx

    • php: docker pull php:7.4-fpm

  • 编辑配置文件

    • default.conf

      server {
          listen       80;        #修改映射端口
          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:
            - "8081:80"                       #修改端口映射
          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: admin            #设置好环境变量,在php代码中使用变量名直接引用
          volumes:
            - ./web:/web/php                  #挂载工作目录到本机web目录
      
       mysql:
         image: mysql_image
         container_name: mysql
         build:
            context: .
            dockerfile: dockerfile_mysql
         ports:
           - "8082:3306"
         volumes:
           - ./mysql_data:/var/lib/mysql       #挂载容器卷,实现数据同步,防止数据丢失
      
    • dockerfile_mysql

      #基础镜像
      FROM mysql
      #作者信息
      MAINTAINER xiaoc
      #设置root密码
      ENV MYSQL_ROOT_PASSWORD admin
      #禁止空密码登录
      ENV MYSQL_ALLOW_EMPTY_PASSWORD no
      
    • dockerfile_php

      #基础镜像
      FROM mysql
      #作者信息
      MAINTAINER xiaoc
      #设置root密码
      ENV MYSQL_ROOT_PASSWORD admin
      #禁止空密码登录
      ENV MYSQL_ALLOW_EMPTY_PASSWORD no
      
    • dockerfile_nginx

      #基础镜像
      FROM nginx
      #作者信息
      MAINTAINER xiaoc
      #声明暴露的端口
      EXPOSE 80
      
    • web文件夹中加入两个文件

      • index.html

        This is a lnmp page test!
        
      • index.php

        <?php phpinfo();?>
        

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

  • 执行docker-compose文件docker-compose up -d --build

  • 查看容器 docker ps -a

  • 分别访问测试页面index.html和index.php

(4)服务测试

  • 创建数据库和表格
<?php
$servername = "mysql";
$username = "root";
$password = "admin";
$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;
?>

  • 向表格插入数据

    <?php
    $servername = "mysql";
    $username = "root";
    $password = "admin";
    $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','小明','男'),
        ('031700001','小红','女'),
        ('031700002','小张','男')");
     
        // 提交事务
        $conn->commit();
        echo "新纪录插入成功";
    }
    catch(PDOException $e)
    {
        // 如果执行失败回滚
        $conn->rollback();
        echo $sql . "<br>" . $e->getMessage();
    }
     
    $conn = null;
    ?>
    

  • 查询表格数据

    <?php
    echo "<table style='border: solid 1px black;'>";
    echo "<tr><th>Id</th><th>Firstname</th><th>Lastname</th></tr>";
     
    class TableRows extends RecursiveIteratorIterator {
        function __construct($it) { 
            parent::__construct($it, self::LEAVES_ONLY); 
        }
     
        function current() {
            return "<td style='width:150px;border:1px solid black;'>" . parent::current(). "</td>";
        }
     
        function beginChildren() { 
            echo "<tr>"; 
        } 
     
        function endChildren() { 
            echo "</tr>" . "\n";
        } 
    } 
     
    $servername = "mysql";
    $username = "root";
    $password = "admin";
    $dbname = "myDB";
     
    try {
        $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $stmt = $conn->prepare("SELECT sno, sname, sex FROM S"); 
        $stmt->execute();
     
        // 设置结果集为关联数组
        $result = $stmt->setFetchMode(PDO::FETCH_ASSOC); 
        foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) { 
            echo $v;
        }
    }
    catch(PDOException $e) {
        echo "Error: " . $e->getMessage();
    }
    $conn = null;
    echo "</table>";
    ?>
    

  • 删除姓名为小明的数据

    <?php
    $servername = "mysql";
    $username = "root";
    $password = "admin";
    $dbname="myDB";
     
    try {
        $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        // 开始事务
        $conn->beginTransaction();
        // SQL DELETE语句
       $conn->exec("DELETE FROM S where sname='小明'");
     
        // 提交事务
        $conn->commit();
        echo "修改删除成功!";
    }
    catch(PDOException $e)
    {
        // 如果执行失败回滚
        $conn->rollback();
        echo $sql . "<br>" . $e->getMessage();
    }
     
    $conn = null;
    ?>
    

(6)实验总结

  • 花费时间 一下午加一晚上 大约7个小时
  • 实验中遇到各种各样的错误,学会了自己分析排查议一些错误,也感谢写完的人以及网上的大神们提供的经验和分析。
posted @ 2020-05-08 15:50  REPLUSONE  阅读(137)  评论(0编辑  收藏  举报