fix: connect docker php to host-only test database

This commit is contained in:
hajimi
2026-08-01 17:24:13 +08:00
parent b490b08c7b
commit 9e44df3a73
5 changed files with 14 additions and 12 deletions
+2 -1
View File
@@ -1,5 +1,6 @@
FROM registry.cn-guangzhou.aliyuncs.com/likeadmin/php:8.0.30.3-fpm
RUN docker-php-ext-install -j"$(nproc)" opcache
RUN docker-php-ext-install -j"$(nproc)" opcache \
&& sed -i 's/^listen = 127\.0\.0\.1:9000$/listen = 0.0.0.0:9000/' /usr/local/etc/php-fpm.d/www.conf
COPY docker/config/php/conf.d/zz-opcache.ini /usr/local/etc/php/conf.d/zz-opcache.ini
@@ -17,7 +17,7 @@ server {
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass server:9000;
fastcgi_pass host.docker.internal:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /likeadmin_php/server/public$fastcgi_script_name;
}
+4 -8
View File
@@ -11,33 +11,29 @@ services:
<<: *php-image
working_dir: /likeadmin_php/server
restart: unless-stopped
network_mode: host
volumes:
- ../server:/likeadmin_php/server
- ./.env.server:/likeadmin_php/server/.env:ro
extra_hosts:
- "host.docker.internal:host-gateway"
networks:
- sport-era-dev
scheduler:
<<: *php-image
working_dir: /likeadmin_php/server
restart: unless-stopped
network_mode: host
command: >-
sh -c "while true; do php think crontab; sleep 60; done"
volumes:
- ../server:/likeadmin_php/server
- ./.env.server:/likeadmin_php/server/.env:ro
extra_hosts:
- "host.docker.internal:host-gateway"
networks:
- sport-era-dev
nginx:
image: registry.cn-guangzhou.aliyuncs.com/likeadmin/nginx:1.23.1
restart: unless-stopped
depends_on:
- server
extra_hosts:
- "host.docker.internal:host-gateway"
ports:
- "8000:80"
volumes:
+2 -1
View File
@@ -20,7 +20,8 @@
完成内容:
- `docker/docker-compose.dev.yml` 移除本地 MySQL、Redis 服务及后端对它们的启动依赖。
- Docker 后端和定时任务通过独立的 `docker/.env.server` 连接测试服宿主机上的 MySQL `test_sbnews` 与 Redis `6377/DB4`
- `docker/.env.server` 作为私有运行配置,不进入 Git 或服务器代码部署包;容器通过 `host.docker.internal` 访问宿主机服务
- PHP 后端使用宿主机网络,使仅允许 `127.0.0.1` 登录的测试 MySQL 用户仍可复用;Nginx 通过 `host.docker.internal:9000` 访问 PHP-FPM
- `docker/.env.server` 作为私有运行配置,不进入 Git 或服务器代码部署包。
涉及模块:
- `docker/docker-compose.dev.yml`
@@ -6,6 +6,7 @@ $files = [
'compose' => $root . '/docker/docker-compose.dev.yml',
'php_dockerfile' => $root . '/docker/Dockerfile.php',
'opcache_ini' => $root . '/docker/config/php/conf.d/zz-opcache.ini',
'nginx_config' => $root . '/docker/config/nginx/conf.d/sport-era.dev.conf',
'uniapp_config' => $root . '/uniapp/src/config/index.ts',
];
@@ -19,19 +20,22 @@ foreach ($files as $label => $path) {
$compose = file_get_contents($files['compose']);
$dockerfile = file_get_contents($files['php_dockerfile']);
$opcacheIni = file_get_contents($files['opcache_ini']);
$nginxConfig = file_get_contents($files['nginx_config']);
$uniappConfig = file_get_contents($files['uniapp_config']);
$expectations = [
[$compose, 'sport-era-dev-php:local', 'server 和 scheduler 应使用本地 PHP 镜像'],
[$compose, 'docker/Dockerfile.php', '本地 PHP 镜像应由 Dockerfile 构建'],
[$compose, './.env.server:/likeadmin_php/server/.env:ro', 'Docker 后端应使用独立的服务器环境文件'],
[$compose, 'host.docker.internal:host-gateway', 'Docker 后端应能访问宿主机上的数据库与 Redis'],
[$compose, 'network_mode: host', 'Docker 后端应使用宿主机网络访问仅允许本机连接的测试数据库'],
[$dockerfile, 'docker-php-ext-install', '本地 PHP 镜像必须安装 OPcache'],
[$dockerfile, '0.0.0.0:9000', 'PHP-FPM 应允许 Nginx 容器通过宿主机网络访问'],
[$dockerfile, 'opcache', '本地 PHP 镜像必须安装 OPcache'],
[$dockerfile, 'zz-opcache.ini', '本地 PHP 镜像必须载入 OPcache 配置'],
[$opcacheIni, 'opcache.enable=1', 'OPcache 必须启用'],
[$opcacheIni, 'opcache.validate_timestamps=1', '开发环境必须保留源码更新时间检测'],
[$opcacheIni, 'opcache.revalidate_freq=0', '源码修改必须立即使 OPcache 失效'],
[$nginxConfig, 'fastcgi_pass host.docker.internal:9000', 'Nginx 应通过宿主机网关访问 host 网络 PHP-FPM'],
[$uniappConfig, 'http://127.0.0.1:8000', 'H5 本地 API 应使用 IPv4 回环地址'],
];