1.目的
将PHP容器与Nginx容器进行通信,开始搭建PHP开发环境
2.配置Nginx
拉取Nginx镜像
localhost:nginx_fpm sukangshen$ docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
743f2d6c1f65: Already exists
6bfc4ec4420a: Pull complete
688a776db95f: Pull complete
Digest: sha256:23b4dcdf0d34d4a129755fc6f52e1c6e23bb34ea011b315d87e193033bcd1b68
Status: Downloaded newer image for nginx:latest
使用nginx镜像开启nginx应用容器
localhost:nginx_fpm sukangshen$ docker run -d --name myNginx -p 8082:80 -v ~/www/my_test/nginx_fpm/html:/usr/share/nginx/html nginx
9da9bbb80a88650ca91a45bc2fa2fdc7dab59b50392ecf34edfd8e3c118d20ef
3.配置PHP
拉取PHP镜像
localhost:nginx_fpm sukangshen$ docker pull php:7.2-fpm
7.2-fpm: Pulling from library/php
743f2d6c1f65: Pull complete
6307e89982cc: Pull complete
807218e72ce2: Pull complete
5108df1d03f8: Pull complete
82ffe3f78be2: Pull complete
17c66bee581c: Pull complete
03011f3bcf78: Pull complete
28b6a4179c3e: Pull complete
b77fa38a9e9c: Pull complete
79928611774d: Pull complete
Digest: sha256:c50c0cffd2af10291d8b8fe3dd410aed3eca302b8163e2ab34cd1bfc6feae495
Status: Downloaded newer image for php:7.2-fpm
使用php-fpm镜像开启php-fpm应用容器
localhost:nginx_fpm sukangshen$ docker run -d --name myFpm -p 9000:9000 -v ~/www/my_test/nginx_fpm/html:/usr/share/nginx/html php:7.2-fpm
e040f89262732ec8e3951ce38c444fa23f5693f943fc1860e98bbec63dec41a1
3.查看是否启动成功
localhost:nginx_fpm sukangshen$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9da9bbb80a88 nginx "nginx -g 'daemon of…" 18 minutes ago Up 18 minutes 0.0.0.0:8082->80/tcp myNginx
e040f8926273 php:7.2-fpm "docker-php-entrypoi…" 18 minutes ago Up 18 minutes 0.0.0.0:9000->9000/tcp myFpm
4.查看PHP的IP地址
docker inspect 容器名 | grep "IPAddress"
localhost:nginx_fpm sukangshen$ docker inspect myFpm | grep "IPAddress"
Error: No such object: myFpm
localhost:nginx_fpm sukangshen$ docker inspect optimistic_varahamihira | grep "IPAddress"
"SecondaryIPAddresses": null,
"IPAddress": "172.17.0.2",
"IPAddress": "172.17.0.2",
5.修改nginx的相关配置
在容器中是没有vim命令的,所以不能在容器中直接修改配置文件。所以我们必须通过变通的方式去解决这个问题,否则只能在每个容器中安装vim。
首先登录到对应的容器中,查看配置信息路径,这在之后修改时会用到。
docker exec -it myNginx /bin/bash
##查看nginx的配置文件
/etc/nginx/conf.d/default.conf
退出命令行,不要使用exit,因为exit会让容器停止。这里使用ctrl + p + q来退出容器。
使用专用的复制命令将配置文件复制到宿主机,然后在宿主机进行编辑(这就是变通的方法)
docker cp myNginx:/etc/nginx/conf.d/default.conf ./default.conf
这里用到了上一步查询到的配置文件路径信息
在宿主机修改配置文件的php部分,内容如下:
location ~ \.php$ {
fastcgi_pass 172.17.0.2:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
include fastcgi_params;
}
再次使用复制命令将其复制到容器中,然后再次进入容器中,将nginx配置文件重新载入
docker cp ./default.conf myNginx:/etc/myNginx:/etc/nginx/conf.d/default.conf
进入到nginx容器中重新载入配置文件
docker exec -it myNginx /bin/bash
service nginx reload