"""Offline verification lab for Docker Ch09: macos-docker-install."""
import json
import re
import shlex
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: Homebrew Cask installation command parser
brew_cmd = "brew install --cask --appdir=/Applications docker"
tokens = shlex.split(brew_cmd)
actual_brew = {
"tool": tokens[0],
"action": tokens[1],
"is_cask": "--cask" in tokens,
"appdir": [t.split("=", 1)[1] for t in tokens if t.startswith("--appdir=")][0],
"target": tokens[-1],
}
expected_brew = {
"tool": "brew",
"action": "install",
"is_cask": True,
"appdir": "/Applications",
"target": "docker",
}
cases.append({
"name": "homebrew_cask_command_validation",
"summary": "Validate Homebrew Cask command structure for installing Docker.app to /Applications.",
"expected": expected_brew,
"actual": actual_brew,
"passed": actual_brew == expected_brew,
})
# Case 2: docker --version output format check (live local CLI + historical format parity)
p_ver = run_docker("--version")
pattern = r"^Docker version [0-9]+\.[0-9]+\.[0-9]+[^\s,]*, build [0-9a-fA-F]+"
sample_hist = "Docker version 17.09.1-ce, build 19e2cf6"
actual_ver = {
"live_rc": p_ver.returncode,
"live_format_match": bool(re.match(pattern, p_ver.stdout.strip())),
"historical_format_match": bool(re.match(pattern, sample_hist)),
}
expected_ver = {"live_rc": 0, "live_format_match": True, "historical_format_match": True}
cases.append({
"name": "docker_version_cli_check",
"summary": "Verify live docker --version matches Docker CLI version/build output format.",
"expected": expected_ver,
"actual": actual_ver,
"passed": actual_ver == expected_ver,
})
# Case 3: docker info RegistryConfig.Mirrors read-only inspection + mirror URL validation
p_info = run_docker("info", "--format", "{{json .RegistryConfig.Mirrors}}")
parsed_mirrors = json.loads(p_info.stdout.strip())
sample_mirror = "http://hub-mirror.c.163.com"
actual_info = {
"info_rc": p_info.returncode,
"mirrors_is_list_or_null": parsed_mirrors is None or isinstance(parsed_mirrors, list),
"sample_mirror_has_scheme": sample_mirror.startswith(("http://", "https://")),
}
expected_info = {
"info_rc": 0,
"mirrors_is_list_or_null": True,
"sample_mirror_has_scheme": True,
}
cases.append({
"name": "docker_info_registry_mirrors_inspection",
"summary": "Query read-only docker info RegistryConfig.Mirrors JSON field offline.",
"expected": expected_info,
"actual": actual_info,
"passed": actual_info == expected_info,
})
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-macos-docker-install-lab/explore-macos-docker-install.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-macos-docker-install.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联30章) · 含当前章节或实例 · mbskycom202612a1457.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1480.zip