"""Offline verification lab for Docker Ch15: repository."""
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: docker login & docker logout CLI help offline
p_login = run_docker("login", "--help")
p_logout = run_docker("logout", "--help")
actual_auth = {
"login_help_ok": p_login.returncode == 0 and "docker login" in p_login.stdout,
"logout_help_ok": p_logout.returncode == 0 and "docker logout" in p_logout.stdout,
}
expected_auth = {"login_help_ok": True, "logout_help_ok": True}
cases.append({
"name": "registry_auth_cli_help_check",
"summary": "Verify docker login and docker logout CLI help offline without authenticating to any remote registry.",
"expected": expected_auth,
"actual": actual_auth,
"passed": actual_auth == expected_auth,
})
# Case 2: docker search & docker pull CLI help + search column schema
p_search = run_docker("search", "--help")
p_pull = run_docker("pull", "--help")
search_cols = ["NAME", "DESCRIPTION", "STARS", "OFFICIAL", "AUTOMATED"]
actual_sp = {
"search_help_ok": p_search.returncode == 0,
"pull_help_ok": p_pull.returncode == 0,
"search_column_count": len(search_cols),
}
expected_sp = {"search_help_ok": True, "pull_help_ok": True, "search_column_count": 5}
cases.append({
"name": "registry_search_and_pull_cli_help",
"summary": "Verify docker search/pull CLI help and validate the 5 docker search output columns.",
"expected": expected_sp,
"actual": actual_sp,
"passed": actual_sp == expected_sp,
})
# Case 3: docker tag / image ls / push CLI help + user namespace validation
p_tag = run_docker("tag", "--help")
p_img_ls = run_docker("image", "ls", "--help")
p_push = run_docker("push", "--help")
ns_re = r"^[a-z0-9][a-z0-9_-]*/[a-z0-9._-]+:[a-zA-Z0-9._-]+$"
actual_ns = {
"tag_ls_push_help_ok": p_tag.returncode == 0 and p_img_ls.returncode == 0 and p_push.returncode == 0,
"namespaced_ref_valid": bool(re.fullmatch(ns_re, "username/ubuntu:18.04")),
"bare_official_needs_namespace": not bool(re.fullmatch(ns_re, "ubuntu:18.04")),
}
expected_ns = {
"tag_ls_push_help_ok": True,
"namespaced_ref_valid": True,
"bare_official_needs_namespace": True,
}
cases.append({
"name": "tag_and_push_namespace_validator",
"summary": "Verify docker tag/image ls/push CLI help and validate username/repo:tag namespace rule for pushing.",
"expected": expected_ns,
"actual": actual_ns,
"passed": actual_ns == expected_ns,
})
# Case 4: Read-only local docker image ls verification offline
p_local = run_docker("image", "ls", "debian:bookworm-slim", "--format", "{{.Repository}}:{{.Tag}}")
actual_local = {"rc": p_local.returncode, "image_ref": p_local.stdout.strip()}
expected_local = {"rc": 0, "image_ref": "debian:bookworm-slim"}
cases.append({
"name": "local_image_ls_read_only_check",
"summary": "Run read-only docker image ls on cached debian:bookworm-slim offline.",
"expected": expected_local,
"actual": actual_local,
"passed": actual_local == expected_local,
})
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-repository-lab/explore-repository.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-repository.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联30章) · 含当前章节或实例 · mbskycom202612a1457.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1482.zip