"""Offline verification lab for Docker Ch07: centos-docker-install."""
import json
import re
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: Legacy RPM cleanup list & convenience script check
legacy_rpms = [
"docker", "docker-client", "docker-client-latest", "docker-common",
"docker-latest", "docker-latest-logrotate", "docker-logrotate", "docker-engine",
]
script_cmds = [
"curl -fsSL https://get.docker.com -o install-docker.sh",
"sudo sh install-docker.sh",
]
actual_old = {"legacy_rpm_count": len(legacy_rpms), "script_step_count": len(script_cmds)}
expected_old = {"legacy_rpm_count": 8, "script_step_count": 2}
cases.append({
"name": "centos_old_packages_and_convenience_script",
"summary": "Validate the 8 legacy CentOS Docker RPM package names and convenience install script steps.",
"expected": expected_old,
"actual": actual_old,
"passed": actual_old == expected_old,
})
# Case 2: DNF repo file rewrite with Tsinghua TUNA mirror (equivalent to sed -i)
official_repo = (
"[docker-ce-stable]\n"
"baseurl=https://download.docker.com/linux/centos/$releasever/$basearch/stable\n"
"gpgkey=https://download.docker.com/linux/centos/gpg\n"
)
rewritten = official_repo.replace(
"https://download.docker.com",
"https://mirrors.tuna.tsinghua.edu.cn/docker-ce",
)
actual_repo = {
"official_occurrences_remaining": rewritten.count("https://download.docker.com"),
"tuna_occurrences": rewritten.count("https://mirrors.tuna.tsinghua.edu.cn/docker-ce"),
"plugin_pkg": "dnf-plugins-core",
}
expected_repo = {
"official_occurrences_remaining": 0,
"tuna_occurrences": 2,
"plugin_pkg": "dnf-plugins-core",
}
cases.append({
"name": "dnf_repo_and_tsinghua_sed_rewrite",
"summary": "Verify sed-equivalent mirror URL replacement on docker-ce.repo from official origin to TUNA mirror.",
"expected": expected_repo,
"actual": actual_repo,
"passed": actual_repo == expected_repo,
})
# Case 3: GPG fingerprint format (40 hex digits), RPM bundle, systemd enable --now, uninstall
gpg_fp = "060A 61C5 1B55 8A7F 742B 77AA C52F EB6B 621E 9F35"
compact_fp = gpg_fp.replace(" ", "")
rpm_bundle = ["docker-ce", "docker-ce-cli", "containerd.io", "docker-buildx-plugin", "docker-compose-plugin"]
actual_gpg = {
"fingerprint_len": len(compact_fp),
"fingerprint_hex_valid": bool(re.fullmatch(r"[0-9A-F]{40}", compact_fp)),
"rpm_bundle_count": len(rpm_bundle),
"enable_now_flag": "--now" in "sudo systemctl enable --now docker",
}
expected_gpg = {
"fingerprint_len": 40,
"fingerprint_hex_valid": True,
"rpm_bundle_count": 5,
"enable_now_flag": True,
}
cases.append({
"name": "gpg_fingerprint_and_systemd_enable",
"summary": "Validate Docker RPM GPG key 40-hex fingerprint, 5 CE RPM packages, and systemctl enable --now.",
"expected": expected_gpg,
"actual": actual_gpg,
"passed": actual_gpg == expected_gpg,
})
# Case 4: Offline container execution equivalent to post-install verification
p_ver = run_docker("--version")
p_run = run_docker("run", "--rm", "--network", "none", "debian:bookworm-slim", "/bin/echo", "centos-verify-ok")
actual_run = {
"cli_ok": p_ver.returncode == 0 and "Docker version" in p_ver.stdout,
"echo_out": p_run.stdout.strip(),
}
expected_run = {"cli_ok": True, "echo_out": "centos-verify-ok"}
cases.append({
"name": "offline_container_verification",
"summary": "Confirm local Docker CLI and ephemeral offline container run cleanly.",
"expected": expected_run,
"actual": actual_run,
"passed": actual_run == expected_run,
})
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-centos-docker-install-lab/explore-centos-docker-install.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-centos-docker-install.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联30章) · 含当前章节或实例 · mbskycom202612a1457.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1458.zip