"""Offline verification lab for Docker Ch08: windows-docker-install."""
import json
import shlex
import shutil
import subprocess
import sys
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: Windows host communicating with Linux Docker Engine inside VM/WSL2
p_info = run_docker("info", "--format", "{{.OSType}}|{{.Architecture}}")
os_type, arch = p_info.stdout.strip().split("|")
actual_bridge = {"host_platform": sys.platform, "engine_os": os_type, "engine_arch": arch}
expected_bridge = {"host_platform": "win32", "engine_os": "linux", "engine_arch": "x86_64"}
cases.append({
"name": "windows_host_linux_engine_bridge",
"summary": "Confirm Windows host runs Docker Engine inside a Linux x86_64 virtualized backend.",
"expected": expected_bridge,
"actual": actual_bridge,
"passed": p_info.returncode == 0 and actual_bridge == expected_bridge,
})
# Case 2: PowerShell Hyper-V enablement command validation
ps_cmd = "Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V -All"
tokens = shlex.split(ps_cmd)
actual_hv = {
"cmdlet": tokens[0],
"feature_name": tokens[tokens.index("-FeatureName") + 1],
"has_online_and_all": ("-Online" in tokens) and ("-All" in tokens),
}
expected_hv = {
"cmdlet": "Enable-WindowsOptionalFeature",
"feature_name": "Microsoft-Hyper-V",
"has_online_and_all": True,
}
cases.append({
"name": "hyperv_powershell_feature_command",
"summary": "Parse and validate the PowerShell Enable-WindowsOptionalFeature command for Microsoft-Hyper-V.",
"expected": expected_hv,
"actual": actual_hv,
"passed": actual_hv == expected_hv,
})
# Case 3: Historical Docker Toolbox 6 components inventory (Win7/Win8)
toolbox_components = [
"Docker CLI",
"Docker Machine",
"Docker Compose",
"Kitematic",
"Docker QuickStart shell",
"Oracle VM Virtualbox",
]
actual_tb = {"component_count": len(toolbox_components), "includes_virtualbox": "Oracle VM Virtualbox" in toolbox_components}
expected_tb = {"component_count": 6, "includes_virtualbox": True}
cases.append({
"name": "toolbox_components_inventory",
"summary": "Validate the 6 bundled components of legacy Docker Toolbox for Windows 7/8.",
"expected": expected_tb,
"actual": actual_tb,
"passed": actual_tb == expected_tb,
})
# Case 4: Live Windows CLI docker version & offline container hello output
p_ver = run_docker("version", "--format", "{{.Client.Os}}/{{.Server.Os}}")
p_run = run_docker("run", "--rm", "--network", "none", "debian:bookworm-slim", "/bin/echo", "Hello from Docker.")
actual_win = {
"client_server_os": p_ver.stdout.strip(),
"container_output": p_run.stdout.strip(),
}
expected_win = {
"client_server_os": "windows/linux",
"container_output": "Hello from Docker.",
}
cases.append({
"name": "windows_cli_version_and_offline_run",
"summary": "Verify docker version client/server OS pair and offline container execution on Windows.",
"expected": expected_win,
"actual": actual_win,
"passed": p_ver.returncode == 0 and p_run.returncode == 0 and actual_win == expected_win,
})
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-windows-docker-install-lab/explore-windows-docker-install.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-windows-docker-install.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联30章) · 含当前章节或实例 · mbskycom202612a1457.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1486.zip