13人参与 • 2026-04-29 • Python
在本地调试或搭建漏洞复现环境时,我们经常会使用到各种公开的 docker 镜像(如 vulhub/、jboss/、portainer/ 等)。但出于以下原因,你可能需要将这些镜像迁移到自己的 docker hub 仓库:
docker-compose.yml 统一管理手动一个个 docker tag + docker push 很繁琐,尤其当镜像数量多、体积大时。因此我写了一个 python 脚本,它可以:
xxx/),打上自己的仓库标签tqdm)显示每一层的上传进度将以下代码保存为 push_images_with_progress.py:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import docker
import sys
from tqdm import tqdm
# ========== 配置 ==========
docker_username = "rockmelodies" # 你的 docker hub 用户名
# =========================
client = docker.from_env()
def get_unique_images_from_all_containers():
"""获取所有容器(包括停止的)使用的镜像名(唯一)"""
containers = client.containers.list(all=true)
images = set()
for c in containers:
# 镜像名格式:repository:tag
image_full = c.image.tags[0] if c.image.tags else f"{c.image.id[:12]}"
images.add(image_full)
return images
def short_image_name(full_name):
"""从 vulhub/nginx:1.11.13 提取 nginx:1.11.13"""
# 去掉第一个 / 之前的所有内容
if '/' in full_name:
return full_name.split('/', 1)[-1]
return full_name
def tag_and_push(image_full):
"""打标签并推送,显示进度条"""
short = short_image_name(image_full)
new_tag = f"{docker_username}/{short}"
print(f"\n🚀 处理镜像: {image_full}")
print(f" → 新标签: {new_tag}")
# 1. 拉取原始镜像(确保本地存在)
try:
client.images.get(image_full)
except docker.errors.imagenotfound:
print(f" ⚠️ 本地未找到 {image_full},尝试拉取...")
client.images.pull(image_full)
# 2. 打标签
img = client.images.get(image_full)
img.tag(new_tag)
# 3. 推送(带进度条)
print(" ⬆️ 开始推送...")
layers_progress = {}
for line in client.images.push(new_tag, stream=true, decode=true):
if 'id' in line and 'progressdetail' in line:
layer_id = line['id']
progress = line['progressdetail']
total = progress.get('total')
current = progress.get('current')
if total and current:
if layer_id not in layers_progress:
layers_progress[layer_id] = tqdm(
total=total, unit='b', unit_scale=true,
desc=f" 层 {layer_id[:12]}", leave=false
)
layers_progress[layer_id].update(current - layers_progress[layer_id].n)
if current >= total:
layers_progress[layer_id].close()
del layers_progress[layer_id]
elif 'status' in line:
status = line['status']
if 'already exists' in status.lower():
tqdm.write(f" ℹ️ {status}")
print(f" ✅ 推送完成: {new_tag}\n")
def main():
print("🔍 获取所有容器使用的镜像...")
images = get_unique_images_from_all_containers()
if not images:
print("❌ 没有找到任何容器镜像。")
sys.exit(1)
print(f"📋 找到 {len(images)} 个唯一镜像:")
for img in images:
print(f" - {img}")
for img in images:
try:
tag_and_push(img)
except exception as e:
print(f"❌ 推送失败 {img}: {e}")
print("\n🎉 所有镜像处理完毕!")
if __name__ == "__main__":
main()pip install docker tqdm
如果提示 pip 未找到,请先确保 python 环境正常(必要时使用 python -m ensurepip --upgrade)。
docker login
输入你的 docker id 和 个人访问令牌(推荐) 或密码。
将 docker_username = "rockmelodies" 改为你自己的 docker hub 用户名。
python3 push_images_with_progress.py
🔍 获取所有容器使用的镜像...
📋 找到 11 个唯一镜像:
- vulhub/comfyui:3.39.1-with-manager
- vulhub/webmin:1.910
...
🚀 处理镜像: vulhub/comfyui:3.39.1-with-manager
→ 新标签: rockmelodies/comfyui:3.39.1-with-manager
⬆️ 开始推送...
层 a1b2c3d4e5f6: 45%|████▌ | 45.2m/100m [00:05<00:06, 8.2mb/s]
ℹ️ layer already exists
✅ 推送完成: rockmelodies/comfyui:3.39.1-with-manager
🎉 所有镜像处理完毕!docker.images.push(stream=true) 获取推送过程的实时输出。layer already exists 并直接跳过,不会重新传输数据。tqdm,为每一个正在上传的镜像层绘制独立的进度条。all=true 包括已停止的)。vulhub/、jboss/、portainer/)。vulhub/nginx:1.11.13 → 你的用户名/nginx:1.11.13a:没有登录或登录凭据无效。执行 docker login 并确认用户名/令牌正确。
a:大型镜像的上传需要时间,进度条可能会短暂没有更新,实际仍在传输。请耐心等待。如果长时间无响应,检查网络连接。
a:可以修改 get_unique_images_from_all_containers() 函数,增加过滤逻辑;或直接在脚本中定义一个白名单列表。
a:登录 https://hub.docker.com/u/你的用户名 查看仓库列表。也可以执行 docker pull 你的用户名/镜像名:标签 测试。
--dry-run 模式,只显示将要推送的镜像而不实际执行。这个 python 脚本让你可以用一条命令将本地所有运行过的 docker 镜像完整迁移到自己的 docker hub 仓库,并实时看到推送进度。而且由于 docker 分层的特性,重复执行也不会造成资源浪费。
如果你也在管理多个漏洞复现环境或自定义镜像,不妨试试这个脚本,让镜像迁移变得轻松又透明。
到此这篇关于用 python 实现 docker 镜像批量推送(带进度条)的文章就介绍到这了,更多相关python docker 镜像批量推送内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
您想发表意见!!点此发布评论
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。
发表评论