第3次实践作业

(1)完成Docker-compose的安装

参考资料:Install Docker Compose

通过命令安装

  1. 运行此命令以下载 Docker 合成的当前稳定版本:

    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
    
  2. 修改权限为可执行:

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

通过pip 安装

sudo apt-get update

# 安装pip
sudo apt-get install python-pip

# 安装docker-compose
sudo pip install docker-compose

测试安装。

$ docker-compose --version
docker-compose version 1.25.5, build 1110ad01

(2)Dockerfile编写

要求:

  • LNMP/LAMP选择一种;
  • 分别构建web、php应用、数据库等镜像服务,php镜像亦有提供php:-apache的变体;
  • 提示:php连接数据库需要安装必要的核心扩展部件(PHP Core Extensions)安装方法可参考官方镜像介绍。

nginx Dockerfile

  • Dockerfile
FROM nginx

# maintainer作者字段
LABEL maintainer="shenkay"
  • nginx conf
server {
    listen       80;
    server_name  localhost;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm index.php;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    location ~ \.php$ {
        fastcgi_pass   php:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /www/$fastcgi_script_name;
        include        fastcgi_params;
    }
}

php Dockerfile

FROM php:7.4-fpm
RUN apt-get update && apt-get install -y \
        libfreetype6-dev \
        libjpeg62-turbo-dev \
        libpng-dev \
    && docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install -j$(nproc) gd \
    && docker-php-ext-install pdo_mysql
ENV MYSQL_ROOT_PASSWORD=123456
  • index.php
<?php phpinfo();?>

mysql Dockerfile

FROM mysql

# maintainer作者字段
LABEL maintainer="shenkay"

#设置root密码
ENV MYSQL_ROOT_PASSWORD 123456

# 不允许空密码登录
ENV MYSQL_ALLOW_EMPTY_PASSWORD no

#创建数据库
ENV MYSQL_DATABASE docker_mysql

#为docker_mysql创建新用户
ENV MYSQL_USER=docker
ENV MYSQL_PASSWORD=123456

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

docker-compose.yml

version: "3"
services:

 php:
    image: php_image
    container_name: php_container
    build: ./php
    environment:
      MYSQL_PASSWORD: 123456
    volumes:
      - ./web:/www

 nginx:
    image: nginx_image
    container_name: nginx_container
    build: ./nginx
    ports:
      - "80:80"
    links:
      - php:php
    volumes:
      - ./web:/usr/share/nginx/html
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf

 mysql:
   image: mysql_image
   container_name: mysql_container
   build: ./mysql
   command: --default-authentication-plugin=mysql_native_password
   ports:
     - "3306:3306"
   volumes:
     - ./mysql_data:/var/lib/mysql
  • tree
.
├── docker-compose.yml
├── mysql
│   └── Dockerfile
├── nginx
│   ├── default.conf
│   └── Dockerfile
├── php
│   └── Dockerfile
└── web
    ├── index.html
    └── index.php
  • docker-compose up -d

  • index.html

  • index.php

(4)服务测试

要求和提示:

  • 包括但不限于测试数据库连接、数据库新建、表新建、表记录的插入修改和删除是否成功(pdo或mysqli均可);

  • 相关测试代码参考PHP与MySQL的教程

  • 数据库连接

<?php
$servername = "mysql";
$username = "root";
$password = $_ENV["MYSQL_ROOT_PASSWORD"];

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

  • 数据库新建
<?php
$servername = "mysql";
$username = "root";
$password = $_ENV["MYSQL_ROOT_PASSWORD"];

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

    // 设置 PDO 错误模式为异常
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    //$sql = "DROP DATABASE myDBPDO";
    //$conn->exec($sql);

    $sql = "CREATE DATABASE myDBPDO";

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

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

$conn = null;
?>

  • 表新建
<?php
$servername = "mysql";
$username = "root";
$password = $_ENV["MYSQL_ROOT_PASSWORD"];
$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 MyGuests (
    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 "数据表 MyGuests 创建成功";
}
catch(PDOException $e)
{
    echo $sql . "<br>" . $e->getMessage();
}

$conn = null;
?>

  • 表记录
<?php
$servername = "mysql";
$username = "root";
$password = $_ENV["MYSQL_ROOT_PASSWORD"];
$dbname = "myDBPDO";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    // 设置 PDO 错误模式,用于抛出异常
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sql = "INSERT INTO MyGuests (firstname, lastname, email)
    VALUES ('John', 'Doe', 'john@example.com')";
    // 使用 exec() ,没有结果返回
    $conn->exec($sql);
    echo "新记录插入成功";
}
catch(PDOException $e)
{
    echo $sql . "<br>" . $e->getMessage();
}

$conn = null;
?>root@ubuntu:~/docker/compose# cat web/insert.php
<?php
$servername = "mysql";
$username = "root";
$password = $_ENV["MYSQL_ROOT_PASSWORD"];
$dbname = "myDBPDO";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    // 设置 PDO 错误模式,用于抛出异常
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sql = "INSERT INTO MyGuests (firstname, lastname, email)
    VALUES ('John', 'Doe', 'john@example.com')";
    // 使用 exec() ,没有结果返回
    $conn->exec($sql);
    echo "新记录插入成功";
}
catch(PDOException $e)
{
    echo $sql . "<br>" . $e->getMessage();
}

$conn = null;
?>

遇到的问题

  • php连接docker运行的mysql,显示(HY000/2002): Connection refused

每个docker运行容器的localhost 127.0.0.1都是自己容器本身,不是mysql容器

所以 $servername 应为容器名 或 具体的地址

<?php
$servername = "mysql"; //容器名
...
?>

nginx的配置文件 nginx.conf 中也是如此

server {
	...
	location ~ \.php$ {
        fastcgi_pass   php:9000; #容器名:端口
    ...
}   
posted @ 2020-05-09 00:55  yanami  阅读(185)  评论(0编辑  收藏  举报