"""Offline Docker CentOS installation workflow and CLI flag verification."""
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: docker pull centos:centos7 explicit tag parsing & EOL migration notes
pull_tokens = shlex.split("docker pull centos:centos7")
repo, tag = pull_tokens[-1].split(":", 1)
actual_1 = {
"repository": repo,
"tag": tag,
"rhel_compatible_alternatives": ["rockylinux", "almalinux", "quay.io/centos/centos:stream9"],
}
expected_1 = {
"repository": "centos",
"tag": "centos7",
"rhel_compatible_alternatives": ["rockylinux", "almalinux", "quay.io/centos/centos:stream9"],
}
cases.append({
"name": "centos_tag_pull_command_spec",
"summary": "解析 docker pull centos:centos7 指定标签语法并登记 CentOS 7 生命周期结束后的 RHEL 兼容替代镜像。",
"expected": expected_1,
"actual": actual_1,
"passed": actual_1 == expected_1,
})
# Case 2: Read-only local docker images check
imgs = run_docker("images", "--format", "{{.Repository}}:{{.Tag}}")
assert imgs.returncode == 0, imgs.stderr
cached_set = {line.strip() for line in imgs.stdout.splitlines() if line.strip()}
actual_2 = {
"docker_images_ok": True,
"centos7_uncached_offline": "centos:centos7" not in cached_set,
}
expected_2 = {
"docker_images_ok": True,
"centos7_uncached_offline": True,
}
cases.append({
"name": "local_docker_images_offline_check",
"summary": "离线只读执行 docker images,验证本地镜像查询正常并确认 centos:centos7 未在离线环境预缓存。",
"expected": expected_2,
"actual": actual_2,
"passed": actual_2 == expected_2,
})
# Case 3: docker run -itd --name centos-test centos:centos7 flag breakdown
run_tokens = shlex.split("docker run -itd --name centos-test centos:centos7")
actual_3 = {
"has_i_t_d": all(ch in run_tokens[2] for ch in ("i", "t", "d")),
"container_name": run_tokens[run_tokens.index("--name") + 1],
"image_ref": run_tokens[-1],
}
expected_3 = {
"has_i_t_d": True,
"container_name": "centos-test",
"image_ref": "centos:centos7",
}
cases.append({
"name": "centos_run_and_exec_flags",
"summary": "验证 docker run -itd --name centos-test centos:centos7 的后台保活参数与容器名称解析。",
"expected": expected_3,
"actual": actual_3,
"passed": actual_3 == expected_3,
})
# Case 4: Real read-only docker run --help verification on local Docker CLI 29.8.0
hlp = run_docker("run", "--help")
assert hlp.returncode == 0, hlp.stderr
actual_4 = {
"supports_interactive": "--interactive" in hlp.stdout,
"supports_tty": "--tty" in hlp.stdout,
"supports_detach": "--detach" in hlp.stdout,
"supports_name": "--name" in hlp.stdout,
}
expected_4 = {
"supports_interactive": True,
"supports_tty": True,
"supports_detach": True,
"supports_name": True,
}
cases.append({
"name": "docker_run_cli_help_flags_check",
"summary": "离线只读检查本地 Docker CLI 29.8.0 的 docker run --help,确认 -i/-t/-d/--name 参数完整支持。",
"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-centos-lab/explore-install-centos.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-install-centos.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1468.zip
课程关联工程包(登记关联30章) · 含当前章节或实例 · mbskycom202612a1457.zip