"""Offline Docker PHP + Nginx FastCGI deployment verification using cached nginx:1.27-alpine."""
import json
import re
import shlex
import shutil
import subprocess
import sys
import tempfile
from pathlib import Path
sys.stdout.reconfigure(encoding="utf-8")
DOCKER_BIN = shutil.which("docker") or r"C:\Program Files\Docker\Docker\resources\bin\docker.exe"
ENV_VERSION = "Windows x64 / CPython 3.12.14 / Docker CLI 29.8.0 offline"
def run_docker(*args: str) -> subprocess.CompletedProcess[str]:
return subprocess.run(
[DOCKER_BIN, *args],
capture_output=True,
text=True,
encoding="utf-8",
timeout=30,
)
def main() -> None:
cases = []
# Case 1: PHP image search / pull tag analysis & EOL migration note
pull_tokens = shlex.split("docker pull php:5.6-fpm")
repo, tag = pull_tokens[-1].split(":", 1)
actual_1 = {
"repository": repo,
"tag": tag,
"is_fpm_variant": tag.endswith("-fpm"),
"modern_recommended_tag": "php:8.4-fpm",
}
expected_1 = {
"repository": "php",
"tag": "5.6-fpm",
"is_fpm_variant": True,
"modern_recommended_tag": "php:8.4-fpm",
}
cases.append({
"name": "php_image_discovery_and_tag_spec",
"summary": "解析 docker search/pull php:5.6-fpm 标签约定并注明 PHP 5.6 历史版本向 php:8.4-fpm 的演进。",
"expected": expected_1,
"actual": actual_1,
"passed": actual_1 == expected_1,
})
# Case 2: PHP-FPM & Nginx dual-container run flags, bind mounts, and --link vs user-defined network
php_run = shlex.split("docker run --name myphp-fpm -v ~/nginx/www:/www -d php:5.6-fpm")
nginx_run = shlex.split(
"docker run --name demo-php-nginx -p 8083:80 -d "
"-v ~/nginx/www:/usr/share/nginx/html:ro "
"-v ~/nginx/conf/conf.d:/etc/nginx/conf.d:ro "
"--link myphp-fpm:php nginx"
)
vols = [nginx_run[i + 1] for i, tok in enumerate(nginx_run) if tok == "-v"]
actual_2 = {
"php_container": php_run[php_run.index("--name") + 1],
"php_volume": php_run[php_run.index("-v") + 1],
"nginx_port_map": nginx_run[nginx_run.index("-p") + 1],
"nginx_ro_mounts": [v.endswith(":ro") for v in vols],
"legacy_link": nginx_run[nginx_run.index("--link") + 1],
}
expected_2 = {
"php_container": "myphp-fpm",
"php_volume": "~/nginx/www:/www",
"nginx_port_map": "8083:80",
"nginx_ro_mounts": [True, True],
"legacy_link": "myphp-fpm:php",
}
cases.append({
"name": "php_fpm_and_nginx_container_run_spec",
"summary": "解析 PHP-FPM 与 Nginx 双容器挂载路径、8083:80 端口映射、只读挂载(:ro)及历史 --link 别名映射。",
"expected": expected_2,
"actual": actual_2,
"passed": actual_2 == expected_2,
})
# Case 3: Real offline Nginx FastCGI server config syntax check via cached nginx:1.27-alpine
source_nginx_conf = """server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm index.php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \\.php$ {
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /www/$fastcgi_script_name;
include fastcgi_params;
}
}
"""
# For offline --network none `nginx -t` (no upstream DNS), test identical structure with 127.0.0.1:9000
offline_test_conf = source_nginx_conf.replace("fastcgi_pass php:9000;", "fastcgi_pass 127.0.0.1:9000;")
with tempfile.TemporaryDirectory() as td:
cf = Path(td) / "test-php.conf"
cf.write_text(offline_test_conf, encoding="utf-8")
tst = run_docker(
"run", "--rm", "--network", "none", "--entrypoint", "nginx",
"-v", f"{cf.resolve()}:/etc/nginx/conf.d/default.conf:ro",
"nginx:1.27-alpine", "-t",
)
assert tst.returncode == 0, tst.stderr
out = tst.stdout + tst.stderr
actual_3 = {
"source_fastcgi_upstream": re.search(r"fastcgi_pass\s+([^;]+);", source_nginx_conf).group(1),
"source_script_filename": re.search(r"SCRIPT_FILENAME\s+([^;]+);", source_nginx_conf).group(1),
"nginx_t_syntax_ok": "syntax is ok" in out,
"nginx_t_test_ok": "test is successful" in out,
}
expected_3 = {
"source_fastcgi_upstream": "php:9000",
"source_script_filename": "/www/$fastcgi_script_name",
"nginx_t_syntax_ok": True,
"nginx_t_test_ok": True,
}
cases.append({
"name": "nginx_fastcgi_config_real_validation",
"summary": "使用本地缓存镜像 nginx:1.27-alpine 离线挂载校验 PHP FastCGI server 配置块,实测 nginx -t 通过并核对 /www 路径契约。",
"expected": expected_3,
"actual": actual_3,
"passed": actual_3 == expected_3,
})
# Case 4: index.php probe script & shared directory contract check
php_src = "<?php\necho phpinfo();\n?>\n"
actual_4 = {
"has_open_tag": php_src.lstrip().startswith("<?php"),
"calls_phpinfo": "phpinfo()" in php_src,
"fastcgi_port": 9000,
"shared_host_dir": "~/nginx/www",
}
expected_4 = {
"has_open_tag": True,
"calls_phpinfo": True,
"fastcgi_port": 9000,
"shared_host_dir": "~/nginx/www",
}
cases.append({
"name": "php_index_script_syntax_check",
"summary": "核对测试入口文件 index.php(phpinfo())基本语法结构及 9000 端口与宿主机共享目录契约。",
"expected": expected_4,
"actual": actual_4,
"passed": actual_4 == expected_4,
})
print(json.dumps({"version": ENV_VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
入口文件:projects/docker-install-php-lab/explore-install-php.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-install-php.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联30章) · 含当前章节或实例 · mbskycom202612a1457.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1473.zip