From 6e0382c3babec963fdac33ee43afbfe7b11b15b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zhero=20=26=20=E5=8A=A9=E7=90=86?= Date: Tue, 1 Dec 2026 17:22:08 +0800 Subject: [PATCH] =?UTF-8?q?fix(candidate):=20=E5=AE=B9=E5=99=A8=E6=9C=AA?= =?UTF-8?q?=E8=BF=90=E8=A1=8C=E6=97=B6=E5=B8=A6=E5=87=BA=E9=80=80=E5=87=BA?= =?UTF-8?q?=E7=A0=81=E4=B8=8E=E6=97=A5=E5=BF=97=E5=B0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit inspect() 原本把「容器没在跑」和「镜像不符」合成一句 expected container is not running,真正的死因(缺时区、DSN 被拒、端口绑定失败)只在 dockerd 和容器 日志里,容器被 down 掉后无从查证。改为分别报未知状态/镜像不符,并附 5 行日志尾。 --- scripts/production_cd.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/scripts/production_cd.py b/scripts/production_cd.py index afbba41..8515449 100644 --- a/scripts/production_cd.py +++ b/scripts/production_cd.py @@ -366,9 +366,30 @@ def image_inspect(image): return json.loads(run(['docker', 'image', 'inspect', image], 'read image identity'))[0] +def container_log_tail(name, lines=5, limit=400): + """Give the failure text a symptom, not just a verdict. + + A non-running container used to surface only as "expected container is not + running"; the actual cause (missing zoneinfo, refused DSN, bad bind) lives in + the container log and was thrown away with the container. + """ + try: + result = subprocess.run(['docker', 'logs', '--tail', str(lines), name], capture_output=True, timeout=15) + except (OSError, subprocess.TimeoutExpired): + return 'no container log' + # docker CLI forwards the container's stderr to its own stderr; keep both. + text = ' '.join((result.stdout + result.stderr).decode('utf-8', 'replace').split()) + return text[-limit:] if text else 'no container log' + + def inspect(name): obj = json.loads(run(['docker', 'container', 'inspect', name], 'read running container'))[0] - require(obj['State']['Status'] == 'running' and matches(IMAGE, obj.get('Image')), 'expected container is not running') + state = obj.get('State') or {} + if state.get('Status') != 'running': + raise CDError(f'{name}: container is {state.get("Status") or "unknown"} ' + f'(exit {state.get("ExitCode")}); log: {container_log_tail(name)}') + require(matches(IMAGE, obj.get('Image')), + f'{name}: running image {obj.get("Image")!r} is not an approved image ID') return obj