"""Offline Docker Apache (httpd) Dockerfile/script analysis & Debian bash syntax check."""
import json
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: search / pull / images check (noting httpd image is uncached offline)
imgs = run_docker("images", "--format", "{{.Repository}}:{{.Tag}}")
assert imgs.returncode == 0, imgs.stderr
cached = {ln.strip() for ln in imgs.stdout.splitlines() if ln.strip()}
actual_1 = {
"search_cmd": shlex.split("docker search httpd"),
"pull_cmd": shlex.split("docker pull httpd"),
"httpd_uncached_offline": not any(img.startswith("httpd:") for img in cached),
}
expected_1 = {
"search_cmd": ["docker", "search", "httpd"],
"pull_cmd": ["docker", "pull", "httpd"],
"httpd_uncached_offline": True,
}
cases.append({
"name": "apache_pull_and_images_spec",
"summary": "校验 docker search/pull/images httpd 命令语法,并确认官方 httpd 镜像未在本地离线环境预缓存。",
"expected": expected_1,
"actual": actual_1,
"passed": actual_1 == expected_1,
})
# Case 2: Historical httpd Dockerfile & httpd-foreground script analysis
host_dirs = ["~/apache/www", "~/apache/logs", "~/apache/conf"]
fg_script = "#!/bin/bash\nset -e\n\n# Apache gets grumpy about PID files pre-existing\nrm -f /usr/local/apache2/logs/httpd.pid\n\nexec httpd -DFOREGROUND\n"
build_tokens = shlex.split("docker build -t httpd .")
actual_2 = {
"host_dir_count": len(host_dirs),
"httpd_prefix": "/usr/local/apache2",
"stdout_stderr_log_fds": ["/proc/self/fd/1", "/proc/self/fd/2"],
"cleans_stale_pid": "rm -f /usr/local/apache2/logs/httpd.pid" in fg_script,
"uses_exec_foreground": "exec httpd -DFOREGROUND" in fg_script,
"build_tag": build_tokens[build_tokens.index("-t") + 1],
}
expected_2 = {
"host_dir_count": 3,
"httpd_prefix": "/usr/local/apache2",
"stdout_stderr_log_fds": ["/proc/self/fd/1", "/proc/self/fd/2"],
"cleans_stale_pid": True,
"uses_exec_foreground": True,
"build_tag": "httpd",
}
cases.append({
"name": "apache_dockerfile_and_foreground_script",
"summary": "解析 Apache 目录规划、Dockerfile 日志重定向至 stdout/stderr 机制及 httpd-foreground 前台启动脚本。",
"expected": expected_2,
"actual": actual_2,
"passed": actual_2 == expected_2,
})
# Case 3: docker run with 3 bind mounts + port 80:80 & docker ps check
run_tokens = shlex.split(
"docker run -p 80:80 "
"-v $PWD/www/:/usr/local/apache2/htdocs/ "
"-v $PWD/conf/httpd.conf:/usr/local/apache2/conf/httpd.conf "
"-v $PWD/logs/:/usr/local/apache2/logs/ "
"-d httpd"
)
mounts = [run_tokens[i + 1] for i, tok in enumerate(run_tokens) if tok == "-v"]
actual_3 = {
"port_mapping": run_tokens[run_tokens.index("-p") + 1],
"mount_count": len(mounts),
"container_targets": [m.split(":", 1)[1] for m in mounts],
"detached": "-d" in run_tokens,
}
expected_3 = {
"port_mapping": "80:80",
"mount_count": 3,
"container_targets": [
"/usr/local/apache2/htdocs/",
"/usr/local/apache2/conf/httpd.conf",
"/usr/local/apache2/logs/",
],
"detached": True,
}
cases.append({
"name": "apache_run_volumes_and_ps_spec",
"summary": "解析 Apache 容器运行的 80:80 端口映射与 htdocs、httpd.conf、logs 三组宿主机挂载契约。",
"expected": expected_3,
"actual": actual_3,
"passed": actual_3 == expected_3,
})
# Case 4: Real ephemeral offline Bash syntax verification of httpd-foreground inside debian:bookworm-slim
with tempfile.TemporaryDirectory() as td:
script_path = Path(td) / "httpd-foreground"
script_path.write_bytes(fg_script.encode("utf-8"))
chk = run_docker(
"run", "--rm", "--network", "none",
"-v", f"{Path(td).resolve()}:/work:ro",
"debian:bookworm-slim", "bash", "-n", "/work/httpd-foreground",
)
assert chk.returncode == 0, chk.stderr
actual_4 = {
"bash_syntax_valid": True,
"stderr_empty": chk.stderr.strip() == "",
}
expected_4 = {
"bash_syntax_valid": True,
"stderr_empty": True,
}
cases.append({
"name": "debian_container_foreground_script_syntax",
"summary": "以 --rm --network none 在本地已缓存的 debian:bookworm-slim 容器内执行 bash -n 实测校验 httpd-foreground 启动脚本语法。",
"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-apache-lab/explore-install-apache.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-install-apache.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联30章) · 含当前章节或实例 · mbskycom202612a1457.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1467.zip