"""Offline Docker Nginx installation verification using cached nginx:1.27-alpine."""
import json
import shlex
import shutil
import subprocess
import sys
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 against locally cached nginx:1.27-alpine
imgs = run_docker("images", "--format", "{{.Repository}}:{{.Tag}}")
assert imgs.returncode == 0, imgs.stderr
cached_nginx = [ln.strip() for ln in imgs.stdout.splitlines() if ln.strip().startswith("nginx:")]
search_columns = ["NAME", "DESCRIPTION", "STARS", "OFFICIAL", "AUTOMATED"]
pull_tokens = shlex.split("docker pull nginx:latest")
actual_1 = {
"search_columns": search_columns,
"pull_target": pull_tokens[-1],
"cached_nginx_present": "nginx:1.27-alpine" in cached_nginx,
}
expected_1 = {
"search_columns": ["NAME", "DESCRIPTION", "STARS", "OFFICIAL", "AUTOMATED"],
"pull_target": "nginx:latest",
"cached_nginx_present": True,
}
cases.append({
"name": "nginx_search_pull_and_images_spec",
"summary": "验证 docker search nginx 输出列、docker pull nginx:latest 命令及本地已缓存 nginx:1.27-alpine 镜像。",
"expected": expected_1,
"actual": actual_1,
"passed": actual_1 == expected_1,
})
# Case 2: docker run --name nginx-test -p 8080:80 -d nginx parsing
run_tokens = shlex.split("docker run --name nginx-test -p 8080:80 -d nginx")
host_port, container_port = run_tokens[run_tokens.index("-p") + 1].split(":", 1)
actual_2 = {
"container_name": run_tokens[run_tokens.index("--name") + 1],
"host_port": int(host_port),
"container_port": int(container_port),
"detached": "-d" in run_tokens,
"image": run_tokens[-1],
}
expected_2 = {
"container_name": "nginx-test",
"host_port": 8080,
"container_port": 80,
"detached": True,
"image": "nginx",
}
cases.append({
"name": "nginx_run_port_mapping_spec",
"summary": "解析 docker run --name nginx-test -p 8080:80 -d nginx 的端口映射(宿主机 8080 -> 容器 80)与后台运行参数。",
"expected": expected_2,
"actual": actual_2,
"passed": actual_2 == expected_2,
})
# Case 3: Read-only docker inspect nginx:1.27-alpine
insp = run_docker("inspect", "nginx:1.27-alpine")
assert insp.returncode == 0, insp.stderr
cfg = json.loads(insp.stdout)[0]["Config"]
actual_3 = {
"exposed_80_tcp": "80/tcp" in (cfg.get("ExposedPorts") or {}),
"entrypoint": cfg.get("Entrypoint"),
"cmd": cfg.get("Cmd"),
}
expected_3 = {
"exposed_80_tcp": True,
"entrypoint": ["/docker-entrypoint.sh"],
"cmd": ["nginx", "-g", "daemon off;"],
}
cases.append({
"name": "nginx_cached_image_inspect",
"summary": "只读检查本地缓存镜像 nginx:1.27-alpine 的 80/tcp 暴露端口、入口脚本与前台守护默认命令。",
"expected": expected_3,
"actual": actual_3,
"passed": actual_3 == expected_3,
})
# Case 4: Real ephemeral offline container run (--rm --network none) testing nginx -v and nginx -t
ver = run_docker("run", "--rm", "--network", "none", "--entrypoint", "nginx", "nginx:1.27-alpine", "-v")
tst = run_docker("run", "--rm", "--network", "none", "--entrypoint", "nginx", "nginx:1.27-alpine", "-t")
assert ver.returncode == 0 and tst.returncode == 0, ver.stderr + tst.stderr
ver_line = (ver.stdout + ver.stderr).strip()
tst_out = (tst.stdout + tst.stderr).strip()
actual_4 = {
"version_prefix_ok": ver_line.startswith("nginx version: nginx/1.27."),
"config_syntax_ok": "syntax is ok" in tst_out,
"config_test_successful": "test is successful" in tst_out,
}
expected_4 = {
"version_prefix_ok": True,
"config_syntax_ok": True,
"config_test_successful": True,
}
cases.append({
"name": "nginx_container_version_and_config_test",
"summary": "以 --rm --network none 离线运行 nginx:1.27-alpine,实测 nginx -v 版本输出与 nginx -t 默认配置校验通过。",
"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-nginx-lab/explore-install-nginx.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-install-nginx.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联30章) · 含当前章节或实例 · mbskycom202612a1457.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1471.zip