补充说明
crane 命令 是 Google 开源项目 go-containerregistry 提供的容器镜像命令行工具,用于高效地复制、列举、打标签和管理远程镜像仓库(registry)中的镜像元数据。它直接与 registry 的 HTTP API 交互,速度快且无需本地 Docker daemon,是镜像仓库之间迁移的利器。
crane 支持 crane copy(跨仓库批量复制镜像)、crane ls(列出仓库中的标签)、crane tag(为镜像补打标签)、crane digest(获取镜像 digest)、crane export(导出镜像文件系统)等常用操作。它使用远程镜像的 manifest 和 config 进行复制,支持自动重试与并发,比 pull 再 push 的方式效率更高。
典型使用场景:把镜像从 docker.io 或 gcr.io 批量迁移到自建仓库或云厂商仓库;清理或整理 registry 中的旧标签;在 CI 中获取镜像 digest 用于幂等部署。注意事项:crane 操作的是镜像元数据与层数据,默认不进行本地解压,因此复制过程非常快;crane copy 默认复制所有平台变体,可用 --platform 限制架构。
语法
crane <子命令> [选项] [参数]
选项
--insecure # 允许与 HTTP(非 TLS)registry 通信
--platform=平台 # 仅处理指定平台,如 linux/amd64
-k, --key=路径 # 使用私钥进行 registry 身份认证
--help # 显示帮助信息
实例
# 将镜像从 docker.io 复制到自建仓库
crane copy docker.io/library/nginx:latest registry.example.com/nginx:latest
# 批量复制整个仓库(保留所有标签)
crane copy docker.io/library/redis gcr.io/my-project/redis
# 列出 gcr.io 某仓库下的所有标签
crane ls gcr.io/my-project/redis
# 为已有镜像添加新标签
crane tag registry.example.com/nginx:latest nginx:v2
# 获取镜像 digest(用于部署脚本校验)
crane digest registry.example.com/nginx:latest
# 将镜像导出为 tar 归档
crane export registry.example.com/nginx:latest nginx-fs.tar