赞
踩
https://blog.csdn.net/u010953609/article/details/89337660
1- 单独建立一个 compose 学习的目录
$ tree lnmp2 -d
lnmp2
├── mysql
│ └── data
│ ├── mysql
│ ├── performance_schema
│ └── sys
├── nginx
│ ├── conf.d
│ └── www
│ └── html
└── phpfpm
2- 编写 Dockerfile
nginx 的 Dockerfile
只有一条
~/compose/lnmp2$ cat ./nginx/Dockerfile
FROM nginx:latest
php 的 Dockerfile
** 我是从之前学习 Dockerfile 的抄写过来的,里面大量的 php 扩展
学习时可以去掉后面的那些
也可以只有一条
~/compose/lnmp2$ cat ./phpfpm/Dockerfile
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
mysql 的 Dockerfile
实际也只有一条
~/compose/lnmp2$ cat ./mysql/Dockerfile
FROM mysql:5.7
分别测试 nginx、php、mysql 镜像和容器运行
只需要测试 image 和 docker run 可以正确运行即可,详细配置放在 compose 模版里面
编写模版
ss -tlnp |grep 3306
ss -tlnp |grep 82
确认好可以使用的端口,本次我使用 3308 和 82 端口来分别对应 mysql 和 nginx
~/compose/lnmp2$ cat docker-compose.yml
version: "3" services: web_server: build: ./nginx container_name: nginx links: - php:php72 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 environment: MYSQL_ROOT_PASSWORD: 123456 ports: - "3308:3306" volumes: - ./mysql/data:/var/lib/mysql php: build: ./phpfpm container_name: php72 links: - db_server:mysql57 ports: - "9000:9000" volumes: - ./nginx/www/html:/var/www/html - ./nginx/conf.d:/etc/nginx/conf.d
运行测试
** 为了看得清晰,可以将之前学习的项目都 down 掉之后进行
因为运行时会产生新的 images 和新的 docker 容器
docker-compose down
在当前目录下
~/compose/lnmp2$ docker-compose up -d
Creating network "lnmp2_default" with the default driver
Creating mysql57 ... done
Creating php72 ... done
Creating nginx ... done
测试
curl 127.0.0.1:82
http://192.168.1.193:82/
第一次 up 之后,检查运行的容器,发现只有 2 个 ,nginx 容器没有起来!
docker ps -s
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES SIZE
d07a05bdc966 lnmp2_php "docker-php-entrypoi…" 26 seconds ago Up 25 seconds 0.0.0.0:9000->9000/tcp php72 0B (virtual 77.4MB)
9018c7da0706 lnmp2_db_server "docker-entrypoint.s…" 27 seconds ago Up 25 seconds 33060/tcp, 0.0.0.0:3308->3306/tcp mysql57 4B (virtual 372MB)
查看 nginx 容器日志
docker logs nginx
2019/04/17 01:47:18 [emerg] 1#1: invalid number of arguments in "fastcgi_pass" directive in /etc/nginx/conf.d/default.conf:23
nginx: [emerg] invalid number of arguments in "fastcgi_pass" directive in /etc/nginx/conf.d/default.conf:23
** 因为我是从前一天的 lnmp 直接 cp 过来目录和 conf.d 的 default.conf
里面的 php 容器名字改变了,修改后又忘记了后面分号
** 修改 default.conf 之后,直接启动 nginx 容器即可,无需再次 up
以下是本次的 nginx 配置文件内容
~/compose/lnmp2$ cat ./nginx/conf.d/default.conf
server { listen 80; server_name localhost; location / { root /var/www/html; index index.html index.htm index.php; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /var/www/html; } location ~ \.php$ { root /var/www/html; # fastcgi_pass 127.0.0.1:9000; # 必须修改为compose模版文件中php容器名字 fastcgi_pass php72:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; # 修改为$document_root include fastcgi_params; } }
*** 前面的 docker-compose.yml 存在一些问题,
1. 使用了老式的 link
2. 测试只涉及到了 nginx 和 php ,没有验证 mysql
3. 验证 mysql 时发现容器之间连接不上
4. 重新学习 docker-compose 命令
修改后的模版文件内容如下:
** 主要修改的地方
1. 去掉 link,改用 depends_on
2. 新建一个networks,名字 new (名字可以随意自行定义)
3. 3 个容器都连接到该networks
~/compose/lnmp3$ cat 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:
新建 2 个 php 文件,分别使用 mysqli 和 pdo 连接 mysql
~/compose/lnmp3/nginx/www/html$ cat mysqli.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 mysqli ...\n"; // 创建连接 $conn = new mysqli($servername, $username, $password); // 检测连接 if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } echo "连接成功\n"; $conn->close(); echo " \n closed now ..\n"; ?>
~/compose/lnmp3/nginx/www/html$ cat 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 ......!\n"; try { $conn = new PDO("mysql:host=$servername;", $username, $password); echo "连接成功"; } catch(PDOException $e) { echo $e->getMessage(); } $conn = null; echo "\nclose now!\n"; ?>
本地测试:
curl 127.0.0.1:82
curl 127.0.0.1:82/mysqli.php
curl 127.0.0.1:82/pdo.php
浏览器测试
http://192.168.1.193:82/pdo.php
http://192.168.1.193:82/mysqli.php
整体从头运行测试一次
docker-compose build --force-rm --no-cache
docker-compose up -d
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。