先编辑 Docker 设置国内镜像源 , 添加多几个避免不能访问DockerHub。
{
"registry-mirrors": [
"https://kuamavit.mirror.aliyuncs.com",
"https://registry.docker-cn.com",
"https://docker.mirrors.ustc.edu.cn"
]
}
因为php-fpm 要装 pdo 、pgsql 驱动 . 所以用的是dockerflle的方式
创建文件1
/service/php/Dockerfile
from php:fpm
# 安装PostgreSQL PDO扩展
RUN apt-get update && apt-get install -y libpq-dev && docker-php-ext-configure pgsql -with-pgsql=/usr/local/pgsql
RUN docker-php-ext-install pdo pdo_pgsql pgsql
创建文件2
/service/service.yml
version: '3'
services:
php-fpm:
build: ./php
container_name: php-fpm
restart: always
volumes:
- ./src:/var/www/html
expose:
- "9000"
networks:
- app-network
db:
image: postgres:latest
environment:
- POSTGRES_DB=mydatabase
- POSTGRES_USER=myuser
- POSTGRES_PASSWORD=mypassword
ports:
- "5432:5432"
volumes:
- ./db-data:/var/lib/postgresql/data
networks:
- app-network
nginx:
image: nginx:latest
restart: always
ports:
- "80:80"
volumes:
- ./web-data:/usr/share/nginx/html
- ./nginx/conf.d:/etc/nginx/conf.d
depends_on:
- php-fpm
command: [ "nginx", "-g", "daemon off;" ]
networks:
- app-network
networks:
app-network:
driver: bridge
根据docker-compose内容 , 可在/service/nginx/conf.d/下创建nginx配置文件,
这边添加一个配置来处理php请求
server {
listen 80;
server_name mydomain.com;
root /var/www/html;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass php-fpm:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_script_name;
}
}
文件目录解析
启动下载
sudo docker compose -f services.yml up