"""Offline verification lab for Docker Ch06: debian-docker-install."""
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: Live Debian Bookworm container os-release & dpkg architecture probe
proc_deb = run_docker(
"run", "--rm", "--network", "none", "debian:bookworm-slim",
"sh", "-c", ". /etc/os-release && printf '%s|%s|%s|' \"$ID\" \"$VERSION_ID\" \"$VERSION_CODENAME\" && dpkg --print-architecture",
)
parts = proc_deb.stdout.strip().split("|")
repo_line = (
f"deb [arch={parts[3]} signed-by=/etc/apt/keyrings/docker.asc] "
f"https://download.docker.com/linux/debian {parts[2]} stable"
) if len(parts) == 4 else ""
actual_deb = {
"os_id": parts[0] if len(parts) > 0 else "",
"version_id": parts[1] if len(parts) > 1 else "",
"codename": parts[2] if len(parts) > 2 else "",
"arch": parts[3] if len(parts) > 3 else "",
"repo_line_valid": repo_line.startswith("deb [arch=amd64 signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian bookworm stable"),
}
expected_deb = {
"os_id": "debian",
"version_id": "12",
"codename": "bookworm",
"arch": "amd64",
"repo_line_valid": True,
}
cases.append({
"name": "debian_codename_inside_real_container",
"summary": "Read /etc/os-release VERSION_CODENAME and dpkg --print-architecture inside cached debian:bookworm-slim.",
"expected": expected_deb,
"actual": actual_deb,
"passed": proc_deb.returncode == 0 and actual_deb == expected_deb,
})
# Case 2: Conflicting legacy packages & APT policy target validation
conflicting = ["docker.io", "docker-doc", "docker-compose", "podman-docker", "containerd", "runc"]
https_deps = ["apt-transport-https", "ca-certificates", "curl", "software-properties-common"]
policy_sample = " 500 https://download.docker.com/linux/debian bookworm/stable amd64 Packages"
actual_old = {
"conflicting_count": len(conflicting),
"https_deps_count": len(https_deps),
"points_to_official_repo": "https://download.docker.com/" in policy_sample,
}
expected_old = {"conflicting_count": 6, "https_deps_count": 4, "points_to_official_repo": True}
cases.append({
"name": "debian_old_packages_and_repo_setup",
"summary": "Validate the 6 conflicting legacy Debian packages, HTTPS APT dependencies, and official repo policy match.",
"expected": expected_old,
"actual": actual_old,
"passed": actual_old == expected_old,
})
# Case 3: Official CE + plugin package bundle & systemd/uninstall commands
ce_pkgs = ["docker-ce", "docker-ce-cli", "containerd.io", "docker-buildx-plugin", "docker-compose-plugin"]
systemd_cmds = ["sudo systemctl start docker", "sudo systemctl enable docker"]
cleanup_cmds = ["sudo apt-get purge docker-ce", "sudo rm -rf /var/lib/docker"]
actual_ce = {
"ce_pkg_count": len(ce_pkgs),
"has_compose_plugin": "docker-compose-plugin" in ce_pkgs,
"systemd_count": len(systemd_cmds),
"cleanup_count": len(cleanup_cmds),
}
expected_ce = {"ce_pkg_count": 5, "has_compose_plugin": True, "systemd_count": 2, "cleanup_count": 2}
cases.append({
"name": "debian_ce_plugins_and_systemd_commands",
"summary": "Validate Debian docker-ce + buildx/compose plugin package list, systemd unit commands, and cleanup steps.",
"expected": expected_ce,
"actual": actual_ce,
"passed": actual_ce == expected_ce,
})
# Case 4: Local docker --version & offline container verification
p_ver = run_docker("--version")
p_run = run_docker("run", "--rm", "--network", "none", "debian:bookworm-slim", "/bin/echo", "debian-docker-ok")
actual_chk = {
"version_prefix_ok": p_ver.returncode == 0 and p_ver.stdout.startswith("Docker version "),
"container_echo": p_run.stdout.strip(),
}
expected_chk = {"version_prefix_ok": True, "container_echo": "debian-docker-ok"}
cases.append({
"name": "docker_version_and_offline_container_check",
"summary": "Verify docker --version and offline container execution on cached debian:bookworm-slim.",
"expected": expected_chk,
"actual": actual_chk,
"passed": actual_chk == expected_chk,
})
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-debian-docker-install-lab/explore-debian-docker-install.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-debian-docker-install.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联30章) · 含当前章节或实例 · mbskycom202612a1457.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1462.zip