Docker Container Rerun
Update a docker run container with a conservative workflow.
Required Inputs
Require both of these from the user:
Treat recreate_command as the source of truth. Do not try to reconstruct missing flags from docker inspect.
Scope
Support only containers originally managed by docker run.
Do not use this skill for:
- - INLINECODE6
- guessing or synthesizing missing run flags
- deleting volumes
- INLINECODE7
- changing environment variables, mounts, ports, labels, or networks unless the user explicitly changed the recreate command
Update Rule
Always compare image Id values, not repo digests.
Use this exact logic:
- 1. Read current image Id from the running container:
docker inspect -f '{{.Image}}' <container_name>
- 2. Extract the image reference from
recreate_command. - Pull the latest version of that image:
docker pull <image>
- 4. Read the latest local image Id:
docker image inspect <image> --format '{{.Id}}'
- 5. Recreate the container only if the two Id values differ.
Safety Rules
Before any destructive action, restate the exact recreate command that will be used.
If recreate_command is missing, ambiguous, or not clearly a docker run command, stop and ask the user to provide a valid full command.
If the image cannot be extracted from recreate_command, stop and ask the user to provide the image explicitly inside the command.
Never silently modify the recreate command.
Prefer this sequence when update is needed:
CODEBLOCK3
Validation of recreate_command
Before using it, verify all of the following:
- - starts with INLINECODE12
- includes an image name as the final image argument before any container command
- clearly targets the same logical container the user wants updated
If the command includes an inline container command after the image, preserve it exactly.
If the command is multiline, preserve it exactly.
Recommended Execution Workflow
- 1. Confirm the target container name.
- Echo back the exact recreate command.
- Extract the image from the recreate command.
- Compare current image Id and latest pulled image Id.
- If Ids match, report that the container is already up to date and do nothing else.
- If Ids differ:
- run
docker stop <container_name>
- run
docker rm <container_name>
- run the exact
recreate_command
- 7. Verify startup with:
docker ps --filter name=<container_name>
docker inspect <container_name>
docker logs --tail 100 <container_name>
- 8. Report status clearly, including whether healthcheck is
healthy, starting, or absent.
Bundled Script
Use the bundled script when you want a deterministic check/apply flow:
CODEBLOCK5
Add --apply only when the user has approved the exact recreate command and actual recreation should happen.
The script will:
- - validate INLINECODE19
- extract the image
- pull the latest image
- compare current vs latest image Id
- optionally stop/remove/recreate
- emit JSON summary with container state, health status, and recent logs
Output Expectations
When reporting results, include:
- - target container name
- extracted image name
- current image Id
- latest image Id
- whether recreation was needed
- post-recreate container state
- health status if present
- any obvious log errors seen in recent logs
Example Pattern
Input:
- -
container_name: INLINECODE21 - INLINECODE22 :
CODEBLOCK6
Expected behavior:
- - extract image INLINECODE23
- compare current container image Id vs pulled latest image Id
- recreate only if the Ids differ
- preserve the recreate command exactly
Notes
When users ask to "update container X", prefer asking for the original docker run command unless it is already documented in memory or provided in the current request.
If the user has a known fixed recreate command for a specific container, prefer using that exact command unchanged.
Docker 容器重新运行
使用保守的工作流程更新 docker run 容器。
必需输入
需要用户提供以下两项内容:
- - containername(容器名称)
- recreatecommand(重建命令)
将 recreate_command 视为唯一可信来源。不要尝试从 docker inspect 中重建缺失的标志参数。
适用范围
仅支持最初由 docker run 管理的容器。
请勿将此技能用于:
- - docker compose
- 猜测或合成缺失的运行标志
- 删除卷
- docker system prune
- 更改环境变量、挂载、端口、标签或网络,除非用户明确更改了重建命令
更新规则
始终比较镜像 Id 值,而非仓库摘要。
使用以下精确逻辑:
- 1. 从运行中的容器读取当前镜像 Id:
bash
docker inspect -f {{.Image}} <容器名称>
- 2. 从 recreate_command 中提取镜像引用。
- 拉取该镜像的最新版本:
bash
docker pull <镜像>
- 4. 读取最新的本地镜像 Id:
bash
docker image inspect <镜像> --format {{.Id}}
- 5. 仅当两个 Id 值不同时才重新创建容器。
安全规则
在执行任何破坏性操作之前,重新说明将要使用的精确重建命令。
如果 recreate_command 缺失、不明确或明显不是 docker run 命令,则停止操作并要求用户提供有效的完整命令。
如果无法从 recreate_command 中提取镜像,则停止操作并要求用户在命令中明确提供镜像。
切勿静默修改重建命令。
需要更新时优先采用以下顺序:
bash
docker stop <容器名称>
docker rm <容器名称>
验证 recreate_command
在使用之前,验证以下所有内容:
- - 以 docker run 开头
- 在任何容器命令之前包含一个作为最终镜像参数的镜像名称
- 明确针对用户想要更新的同一逻辑容器
如果命令在镜像后包含内联容器命令,则精确保留它。
如果命令是多行的,则精确保留它。
推荐执行工作流程
- 1. 确认目标容器名称。
- 回显精确的重建命令。
- 从重建命令中提取镜像。
- 比较当前镜像 Id 和最新拉取的镜像 Id。
- 如果 Id 匹配,报告容器已是最新状态,不执行任何其他操作。
- 如果 Id 不同:
- 运行 docker stop <容器名称>
- 运行 docker rm <容器名称>
- 运行精确的 recreate_command
- 7. 使用以下命令验证启动:
bash
docker ps --filter name=<容器名称>
docker inspect <容器名称>
docker logs --tail 100 <容器名称>
- 8. 清晰报告状态,包括健康检查是 healthy、starting 还是不存在。
捆绑脚本
当您想要确定性检查/应用流程时,使用捆绑脚本:
bash
python3 scripts/updatedockerrun_container.py \
--container-name <容器名称> \
--recreate-command <完整的 docker run 命令>
仅当用户已批准精确的重建命令且应实际执行重建时,添加 --apply。
该脚本将:
- - 验证 recreate_command
- 提取镜像
- 拉取最新镜像
- 比较当前镜像 Id 与最新镜像 Id
- 可选地停止/删除/重建
- 输出包含容器状态、健康状态和最近日志的 JSON 摘要
输出预期
报告结果时,包括:
- - 目标容器名称
- 提取的镜像名称
- 当前镜像 Id
- 最新镜像 Id
- 是否需要重建
- 重建后的容器状态
- 健康状态(如果存在)
- 最近日志中看到的任何明显日志错误
示例模式
输入:
- - containername:my-container
- recreatecommand:
bash
docker run -d --network host --name my-container --restart unless-stopped -v example
data:/data -v examplecerts:/etc/ssl/certs -e DB
HOST=<数据库主机> -e DBPORT=<数据库端口> -e DB
NAME=<数据库名称> -e DBUSER=<数据库用户> -e DB_PASSWORD=<数据库密码> --health-cmd=/bin/check-health --health-interval=600s --health-retries=5 --health-timeout=3s example/image:latest
预期行为:
- - 提取镜像 example/image:latest
- 比较当前容器镜像 Id 与拉取的最新镜像 Id
- 仅当 Id 不同时才重建
- 精确保留重建命令
注意事项
当用户要求更新容器 X时,优先要求提供原始的 docker run 命令,除非该命令已记录在内存中或已在当前请求中提供。
如果用户对特定容器有已知的固定重建命令,优先使用该命令而不做任何更改。