文档说明:只记录关键地方;
获得nginx和php基础镜像的内默认配置文件
从容器镜像中拷贝文件到容器外
#!/bin/bash
set -eux
__DIR__=$(
cd "$(dirname "$0")"
pwd
)
cd ${__DIR__}
mkdir -p conf
image=nginx:alpine
container_id=$(docker create $image) # returns container ID
docker cp $container_id:/etc/nginx/nginx.conf conf/nginx-default.conf
docker cp $container_id:/etc/nginx/conf.d/default.conf conf/nginx-default-domain.conf
docker rm $container_id
image=wenba100xie/php:7.1-fpm-alpine-202008121444
container_id=$(docker create $image) # returns container ID
docker cp $container_id:/usr/local/etc/php-fpm.conf conf/7.1-fpm-alpine-php-fpm.conf
docker cp $container_id:/usr/local/etc/php/php.ini conf/7.1-fpm-alpine-php.ini
lsdocker cp $container_id:/usr/local/etc/php-fpm.d/www.conf conf/7.1-fpm-alpine-www.conf
docker rm $container_id
cp conf/7.1-fpm-alpine-www.conf default.conf
cp conf/7.1-fpm-alpine-www.conf www.conf
修改配置: nginx 配置 关键点
/etc/nginx/conf.d/default.conf
root /usr/share/nginx/html;
location /
index index.html index.htm index.php;
}
location ~* \.php$ {
fastcgi_index index.php;
# fastcgi_pass 127.0.0.1:9000;
fastcgi_pass php-fpm:9000; # 关键点:使用服务名称连接,而不是IP
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
修改配置: php-fpm配置 关键点
/usr/local/etc/php-fpm.d/www.conf
; listen = 127.0.0.1:9000 ; 注释默认配置
listen = 9000 ;这里是配置关键点
nginx 容器与PHP容器连接
容器之间网络默认使用 桥接 的方式连接
如果是K8S,nginx配置 fastcgi_pass 127.0.0.1:9000 这里不用修改
version: "3"
services:
nginx-web:
image: nginx:alpine
container_name: nginx-web-server
ports:
- "80:80"
depends_on:
- php-fpm
volumes:
- ./web/:/usr/share/nginx/html/
- ./default.conf:/etc/nginx/conf.d/default.conf
php-fpm:
image: wenba100xie/php:7.1-fpm-alpine-202008121444
container_name: php-fpm-server
expose:
- 9000
volumes:
- ./web/:/usr/share/nginx/html/
- ./www.conf:/usr/local/etc/php-fpm.d/www.conf
启动、关闭、查看日志
# 启动
docker-compose -f docker-compose.yaml up -d
# 关闭
docker-compose -f docker-compose.yaml down --remove-orphans
# 查看日志
docker-compose -f docker-compose.yaml logs -f
参考文档
- php Documentation
- php 所支持的时区列表
- nginx 配置php解释器参考
- Compose file
- 容器的5种网络模式
- 构建PHP容器
- 构建php的两种模式(php-fpm和php-cli)的运行环境