蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

校验 Ubuntu 镜像拉取/运行参数并在同源 Debian 容器离线实测基础环境

PYTHONWindows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。explore-install-ubuntu.py
explore-install-ubuntu.py126 行
"""Offline Docker Ubuntu installation workflow check with cached Debian-family container probe."""
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 normalize_image_ref(ref: str) -> tuple[str, str]:
    if ":" in ref:
        repo, tag = ref.rsplit(":", 1)
        return repo, tag
    return ref, "latest"


def main() -> None:
    cases = []

    # Case 1: docker pull ubuntu vs docker pull ubuntu:latest tag normalization
    ref_short = normalize_image_ref(shlex.split("docker pull ubuntu")[-1])
    ref_explicit = normalize_image_ref(shlex.split("docker pull ubuntu:latest")[-1])
    actual_1 = {
        "short_normalized": list(ref_short),
        "explicit_normalized": list(ref_explicit),
        "equivalent": ref_short == ref_explicit,
    }
    expected_1 = {
        "short_normalized": ["ubuntu", "latest"],
        "explicit_normalized": ["ubuntu", "latest"],
        "equivalent": True,
    }
    cases.append({
        "name": "ubuntu_image_pull_and_tag_resolution",
        "summary": "验证 docker pull ubuntu 与 docker pull ubuntu:latest 默认标签解析完全等价。",
        "expected": expected_1,
        "actual": actual_1,
        "passed": actual_1 == expected_1,
    })

    # Case 2: Read-only local docker images list check
    imgs = run_docker("images", "--format", "{{.Repository}}:{{.Tag}}")
    assert imgs.returncode == 0, imgs.stderr
    img_list = [line.strip() for line in imgs.stdout.splitlines() if line.strip()]
    actual_2 = {
        "docker_images_ok": True,
        "debian_cached_for_offline_probe": "debian:bookworm-slim" in img_list,
    }
    expected_2 = {
        "docker_images_ok": True,
        "debian_cached_for_offline_probe": True,
    }
    cases.append({
        "name": "local_docker_images_list_check",
        "summary": "离线只读执行 docker images,确认本地镜像查询正常且 Debian 同源基础镜像已缓存可用于容器验证。",
        "expected": expected_2,
        "actual": actual_2,
        "passed": actual_2 == expected_2,
    })

    # Case 3: docker run -itd --name ubuntu-test ubuntu flags & exec/ps workflow
    tokens = shlex.split("docker run -itd --name ubuntu-test ubuntu")
    actual_3 = {
        "flags_combined": tokens[2],
        "has_i_t_d": all(ch in tokens[2] for ch in ("i", "t", "d")),
        "container_name": tokens[tokens.index("--name") + 1],
        "image": tokens[-1],
    }
    expected_3 = {
        "flags_combined": "-itd",
        "has_i_t_d": True,
        "container_name": "ubuntu-test",
        "image": "ubuntu",
    }
    cases.append({
        "name": "ubuntu_run_flags_and_exec_workflow",
        "summary": "解析 docker run -itd --name ubuntu-test ubuntu 的交互终端与后台保活参数组合。",
        "expected": expected_3,
        "actual": actual_3,
        "passed": actual_3 == expected_3,
    })

    # Case 4: Ephemeral offline container probe using cached debian:bookworm-slim (--rm --network none)
    probe = run_docker(
        "run", "--rm", "--network", "none",
        "debian:bookworm-slim",
        "sh", "-c", "grep '^ID=' /etc/os-release && dpkg --version | head -n 1",
    )
    assert probe.returncode == 0, probe.stderr
    lines = [ln.strip() for ln in probe.stdout.splitlines() if ln.strip()]
    actual_4 = {
        "os_id_line": lines[0],
        "has_dpkg": "Debian 'dpkg'" in lines[1],
    }
    expected_4 = {
        "os_id_line": "ID=debian",
        "has_dpkg": True,
    }
    cases.append({
        "name": "debian_family_container_offline_probe",
        "summary": "以 --rm --network none 在本地已缓存的同源 debian:bookworm-slim 容器内验证 /etc/os-release 与 dpkg 包管理基础环境。",
        "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-ubuntu-lab/explore-install-ubuntu.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-install-ubuntu.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:Docker

课程关联工程包(登记关联30章) · 含当前章节或实例 · mbskycom202612a1457.zip

下载课程关联工程包:Docker

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1477.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习