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

校验 Docker Machine 历史命令规范并离线实测现代 docker context

PYTHONWindows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。explore-machine.py
explore-machine.py172 行
"""Offline Docker Machine historical CLI grammar, comparison matrix, and modern Docker Context check."""
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 main() -> None:
    cases = []

    # Case 1: 13-row comparison matrix (Docker Machine vs Docker Desktop)
    comparison_keys = [
        "产品定位",
        "主要用途",
        "典型使用时代",
        "macOS / Windows 支持",
        "是否需要额外虚拟化软件",
        "Docker Engine 管理",
        "使用复杂度",
        "是否提供 GUI",
        "Kubernetes 支持",
        "多 Docker Host 管理",
        "适合生产环境",
        "官方维护状态",
        "当前推荐度",
    ]
    actual_1 = {
        "dimension_count": len(comparison_keys),
        "machine_deprecated": True,
        "desktop_recommended_for_local_dev": True,
        "neither_for_production_orchestration": True,
    }
    expected_1 = {
        "dimension_count": 13,
        "machine_deprecated": True,
        "desktop_recommended_for_local_dev": True,
        "neither_for_production_orchestration": True,
    }
    cases.append({
        "name": "machine_vs_desktop_comparison_matrix",
        "summary": "核对 Docker Machine 与 Docker Desktop 的 13 项维度对比结论及历史弃用/替代定位。",
        "expected": expected_1,
        "actual": actual_1,
        "passed": actual_1 == expected_1,
    })

    # Case 2: Historical v0.16.0 install commands across Linux, macOS, Windows Git BASH
    release_tag = "v0.16.0"
    targets = {
        "linux": "/usr/local/bin/docker-machine",
        "macos": "/usr/local/bin/docker-machine",
        "windows_git_bash": "$HOME/bin/docker-machine.exe",
    }
    version_output = "docker-machine version 0.16.0, build 9371605"
    actual_2 = {
        "release_tag": release_tag,
        "platform_count": len(targets),
        "windows_ext_exe": targets["windows_git_bash"].endswith(".exe"),
        "version_matches_tag": "0.16.0" in version_output,
    }
    expected_2 = {
        "release_tag": "v0.16.0",
        "platform_count": 3,
        "windows_ext_exe": True,
        "version_matches_tag": True,
    }
    cases.append({
        "name": "machine_historical_install_scripts",
        "summary": "校验 Linux/macOS/Windows Git BASH 三平台历史安装路径与 v0.16.0 版本校验输出格式。",
        "expected": expected_2,
        "actual": actual_2,
        "passed": actual_2 == expected_2,
    })

    # Case 3: VirtualBox lifecycle commands (ls, create, ip, stop, start, ssh)
    lifecycle = [
        "docker-machine ls",
        "docker-machine create --driver virtualbox test",
        "docker-machine ip test",
        "docker-machine stop test",
        "docker-machine start test",
        "docker-machine ssh test",
    ]
    parsed_subcmds = [shlex.split(c)[1] for c in lifecycle]
    create_tokens = shlex.split(lifecycle[1])
    actual_3 = {
        "subcommands": parsed_subcmds,
        "driver": create_tokens[create_tokens.index("--driver") + 1],
        "machine_name": create_tokens[-1],
    }
    expected_3 = {
        "subcommands": ["ls", "create", "ip", "stop", "start", "ssh"],
        "driver": "virtualbox",
        "machine_name": "test",
    }
    cases.append({
        "name": "machine_vm_lifecycle_commands",
        "summary": "验证 VirtualBox 驱动下 docker-machine ls/create/ip/stop/start/ssh 的命令语法与参数位置。",
        "expected": expected_3,
        "actual": actual_3,
        "passed": actual_3 == expected_3,
    })

    # Case 4: active host resolution via DOCKER_HOST & full 22 subcommands inventory
    hosts = [
        {"name": "dev", "active": "-", "driver": "virtualbox", "state": "Running", "url": "tcp://192.168.99.103:2376"},
        {"name": "staging", "active": "*", "driver": "digitalocean", "state": "Running", "url": "tcp://203.0.113.81:2376"},
    ]
    active_host = next(h for h in hosts if h["active"] == "*")
    subcommands_22 = [
        "active", "config", "create", "env", "inspect", "ip", "kill", "ls",
        "provision", "regenerate-certs", "restart", "rm", "ssh", "scp",
        "mount", "start", "status", "stop", "upgrade", "url", "version", "help",
    ]
    actual_4 = {
        "active_name": active_host["name"],
        "docker_host_url": active_host["url"],
        "subcommand_count": len(subcommands_22),
        "all_unique": len(set(subcommands_22)) == len(subcommands_22),
    }
    expected_4 = {
        "active_name": "staging",
        "docker_host_url": "tcp://203.0.113.81:2376",
        "subcommand_count": 22,
        "all_unique": True,
    }
    cases.append({
        "name": "machine_active_env_and_subcommands",
        "summary": "验证多主机环境中 active 标记与 DOCKER_HOST 对应关系,并核对全部 22 个 docker-machine 子命令清单。",
        "expected": expected_4,
        "actual": actual_4,
        "passed": actual_4 == expected_4,
    })

    # Case 5: Real local read-only `docker context ls` check (modern replacement)
    ctx = subprocess.run(
        [DOCKER_BIN, "context", "ls", "--format", "{{.Name}}"],
        capture_output=True,
        text=True,
        encoding="utf-8",
        timeout=20,
    )
    assert ctx.returncode == 0, ctx.stderr
    contexts = [line.strip() for line in ctx.stdout.splitlines() if line.strip()]
    actual_5 = {
        "has_default_context": "default" in contexts,
        "context_count_positive": len(contexts) >= 1,
    }
    expected_5 = {
        "has_default_context": True,
        "context_count_positive": True,
    }
    cases.append({
        "name": "modern_docker_context_offline_check",
        "summary": "在本地 Docker Engine 29.8.0 离线执行只读 docker context ls,验证现代 Docker Context 多端点管理能力。",
        "expected": expected_5,
        "actual": actual_5,
        "passed": actual_5 == expected_5,
    })

    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-machine-lab/explore-machine.py

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

下载课程关联工程包:Docker

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

下载课程关联工程包:Docker

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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