#!/usr/bin/env python3 """通过 Docker Engine API 拉取 linux/amd64 基础镜像。 本机访问 Docker Hub 会超时。docker pull 在非交互终端里可能一直没有输出, 所以直接把请求发给引擎,再打上构建用的官方名字。 """ import json import socket import sys import urllib.parse SOCK = "/Users/zlwu/.docker/run/docker.sock" IMAGES = [ ("docker.1ms.run/library/nginx", "1.27-alpine", "nginx:1.27-alpine"), ("docker.1ms.run/library/mysql", "8.0", "mysql:8.0"), ("docker.1ms.run/library/redis", "7.4-alpine", "redis:7.4-alpine"), ("docker.1ms.run/library/python", "3.12-slim-bookworm", "python:3.12-slim-bookworm"), ("docker.1ms.run/library/node", "22-bookworm-slim", "node:22-bookworm-slim"), ("docker.1panel.live/minio/minio", "latest", "minio/minio:latest"), ] def request(method: str, path: str, body: bytes = b"") -> bytes: header = ( f"{method} {path} HTTP/1.1\r\n" "Host: localhost\r\n" "Connection: close\r\n" f"Content-Length: {len(body)}\r\n" "\r\n" ).encode() sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) sock.settimeout(900) sock.connect(SOCK) sock.sendall(header + body) chunks = [] while True: try: data = sock.recv(65536) except socket.timeout: raise SystemExit(f"引擎没有在时限内完成 {path}") if not data: break chunks.append(data) sock.close() raw = b"".join(chunks) head, _, payload = raw.partition(b"\r\n\r\n") status = head.split(b" ", 2)[1] if status != b"200" and status != b"201": raise SystemExit(f"{path} -> {status.decode()} {payload[:400]!r}") return payload def stream(method: str, path: str) -> None: header = ( f"{method} {path} HTTP/1.1\r\n" "Host: localhost\r\n" "Connection: close\r\n" "Content-Length: 0\r\n" "\r\n" ).encode() sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) sock.settimeout(900) sock.connect(SOCK) sock.sendall(header) buffer = b"" header_done = False error = None last = "" while True: try: data = sock.recv(65536) except socket.timeout: sock.close() raise SystemExit(f"引擎没有在时限内完成 {path}") if not data: break buffer += data if not header_done: if b"\r\n\r\n" not in buffer: continue head, buffer = buffer.split(b"\r\n\r\n", 1) status = head.split(b" ", 2)[1] if status not in (b"200", b"201"): sock.close() raise SystemExit(f"{path} -> {status.decode()} {buffer[:400]!r}") header_done = True while b"\n" in buffer: line, buffer = buffer.split(b"\n", 1) line = line.strip() if not line.startswith(b"{"): continue try: item = json.loads(line) except json.JSONDecodeError: continue if "error" in item: error = item["error"] status_text = item.get("status") or "" if status_text and status_text != last: last = status_text print(f" {status_text}", flush=True) sock.close() if error: raise SystemExit(error) def pull(source: str, tag: str, official: str) -> None: query = urllib.parse.urlencode( {"fromImage": source, "tag": tag, "platform": "linux/amd64"} ) print(f"拉取 {source}:{tag}", flush=True) stream("POST", f"/v1.47/images/create?{query}") source_ref = f"{source}:{tag}" repo, official_tag = official.split(":", 1) tag_query = urllib.parse.urlencode({"repo": repo, "tag": official_tag}) encoded = urllib.parse.quote(source_ref, safe="") request("POST", f"/v1.47/images/{encoded}/tag?{tag_query}") print(f"已标记 {official}", flush=True) def main() -> None: for source, tag, official in IMAGES: pull(source, tag, official) print("BASE_IMAGES_OK", flush=True) if __name__ == "__main__": try: main() except BrokenPipeError: sys.exit(1)