"""Offline verification lab for Docker Ch03: architecture."""
import json
import shutil
import subprocess
DOCKER_BIN = shutil.which("docker") or r"C:\Program Files\Docker\Docker\resources\bin\docker.exe"
VERSION_LABEL = "Windows x64 / CPython 3.12.14 / Docker CLI 29.8.0 offline"
def run_docker(*args: str, timeout: int = 30) -> subprocess.CompletedProcess:
return subprocess.run(
[DOCKER_BIN, *args],
capture_output=True,
text=True,
encoding="utf-8",
timeout=timeout,
)
def main() -> None:
cases = []
# Case 1: C/S architecture and OOP analogy (1 image -> 2 distinct container instances)
p_ver = run_docker("version", "--format", "{{.Client.APIVersion}}|{{.Server.APIVersion}}")
c_api, s_api = p_ver.stdout.strip().split("|")
p_h1 = run_docker("run", "--rm", "--network", "none", "debian:bookworm-slim", "hostname")
p_h2 = run_docker("run", "--rm", "--network", "none", "debian:bookworm-slim", "hostname")
h1, h2 = p_h1.stdout.strip(), p_h2.stdout.strip()
actual_cs = {
"has_api_versions": bool(c_api and s_api),
"distinct_instances": bool(h1 and h2 and h1 != h2),
"oop_pairs": {"容器": "对象", "镜像": "类"},
}
expected_cs = {
"has_api_versions": True,
"distinct_instances": True,
"oop_pairs": {"容器": "对象", "镜像": "类"},
}
cases.append({
"name": "cs_architecture_and_oop_analogy",
"summary": "Verify Client/Server API versions and two distinct ephemeral instances created from one image.",
"expected": expected_cs,
"actual": actual_cs,
"passed": actual_cs == expected_cs,
})
# Case 2: Client core workflow commands (run, ps, build, exec, pull, push)
cmds = ["run", "ps", "build", "exec", "pull", "push"]
help_ok = {c: (run_docker(c, "--help").returncode == 0) for c in cmds}
expected_help = {c: True for c in cmds}
cases.append({
"name": "client_core_commands_and_workflow",
"summary": "Verify CLI availability for docker run/ps/build/exec/pull/push workflow commands.",
"expected": expected_help,
"actual": help_ok,
"passed": help_ok == expected_help,
})
# Case 3: Multi-container Compose YAML schema & local compose plugin check
sample_compose = {
"version": "3",
"services": {
"web": {"image": "nginx", "ports": ["8080:80"]},
"db": {"image": "mysql", "environment": {"MYSQL_ROOT_PASSWORD": "example"}},
},
}
p_comp = run_docker("compose", "version")
actual_comp = {
"service_names": sorted(sample_compose["services"].keys()),
"web_port": sample_compose["services"]["web"]["ports"][0],
"compose_cli_available": p_comp.returncode == 0,
}
expected_comp = {
"service_names": ["db", "web"],
"web_port": "8080:80",
"compose_cli_available": True,
}
cases.append({
"name": "compose_yaml_definition_check",
"summary": "Validate web+db Compose service definition and confirm local docker compose CLI responds.",
"expected": expected_comp,
"actual": actual_comp,
"passed": actual_comp == expected_comp,
})
# Case 4: Swarm & Docker Networks (bridge, host, overlay)
p_swarm = run_docker("swarm", "init", "--help")
p_net_ls = run_docker("network", "ls", "--format", "{{.Driver}}")
drivers = set(p_net_ls.stdout.strip().splitlines())
p_net_create = run_docker("network", "create", "--help")
actual_net = {
"swarm_init_help_ok": p_swarm.returncode == 0,
"builtin_bridge_and_host": {"bridge", "host"}.issubset(drivers),
"network_create_driver_flag": "--driver" in p_net_create.stdout,
}
expected_net = {
"swarm_init_help_ok": True,
"builtin_bridge_and_host": True,
"network_create_driver_flag": True,
}
cases.append({
"name": "swarm_and_network_modes_check",
"summary": "Verify docker swarm init help, built-in bridge/host drivers, and network create --driver support.",
"expected": expected_net,
"actual": actual_net,
"passed": actual_net == expected_net,
})
# Case 5: Docker Volumes CLI & mount syntax validation
p_vol = run_docker("volume", "create", "--help")
p_run_help = run_docker("run", "--help")
mount_spec = "my_volume:/data"
vol_name, target_path = mount_spec.split(":", 1)
actual_vol = {
"volume_create_help_ok": p_vol.returncode == 0,
"run_supports_volume_flag": "--volume" in p_run_help.stdout,
"parsed_mount": {"volume": vol_name, "target": target_path},
}
expected_vol = {
"volume_create_help_ok": True,
"run_supports_volume_flag": True,
"parsed_mount": {"volume": "my_volume", "target": "/data"},
}
cases.append({
"name": "volume_persistence_and_help_check",
"summary": "Verify docker volume create and docker run --volume CLI flags plus named-volume mount parsing.",
"expected": expected_vol,
"actual": actual_vol,
"passed": actual_vol == expected_vol,
})
print(json.dumps({"version": VERSION_LABEL, "cases": cases}, ensure_ascii=False))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
入口文件:projects/docker-architecture-lab/explore-architecture.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-architecture.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联30章) · 含当前章节或实例 · mbskycom202612a1457.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1456.zip