镜像搬运工Skopeo工具妙用

Skopeo 是一个用于操作容器镜像和仓库的命令行工具,支持在不运行完整容器引擎(如 Docker)的情况下,直接与镜像仓库交互。它支持多种镜像格式(如 Docker、OCI)和存储后端(如本地目录、容器存储),常用于镜像复制、检查、同步和删除等任务。


主要方法及使用示例

1. copy:复制镜像

将镜像从源复制到目标,支持不同仓库和存储类型之间的传输。

# 从 Docker Hub 复制到本地目录(OCI 格式)
skopeo copy docker://alpine:latest dir:./alpine-local

# 复制到私有仓库(需认证)
skopeo copy \
  --src-creds=user:password \
  --dest-creds=user:password \
  docker://alpine:latest \
  docker://private-registry.example.com/alpine:latest

# 从本地容器存储复制到远程仓库
skopeo copy containers-storage:alpine:latest docker://registry.example.com/alpine:latest

2. inspect:查看镜像元数据

获取镜像的详细信息(如架构、层、环境变量等)。

# 查看 Docker Hub 上的镜像信息
skopeo inspect docker://nginx:latest

# 显示原始 JSON 输出
skopeo inspect --raw docker://alpine:latest

3. list-tags:列出镜像的所有标签

列出远程仓库中某个镜像的所有可用标签。

# 列出 Docker Hub 上的镜像标签
skopeo list-tags docker://docker.io/library/ubuntu

4. delete:删除远程仓库中的镜像

需要仓库支持 Docker Registry API v2 的删除功能。

# 删除私有仓库中的镜像(需认证)
skopeo delete \
  --creds=admin:password \
  docker://registry.example.com/myapp:v1.0

5. sync:批量同步镜像

根据配置文件或命令行参数同步多个镜像。

示例配置文件 sync-config.yaml

# 同步多个镜像到本地目录
registry.example.com:
  images:
    nginx:
      - latest
      - alpine
    busybox: []
# 执行同步(从远程仓库到本地目录)
skopeo sync \
  --src yaml \
  --dest dir \
  sync-config.yaml \
  ./local-images

6. manifest-digest:计算清单摘要

生成镜像清单(manifest)的 SHA256 摘要。

skopeo manifest-digest nginx.json

7. login/logout:认证到仓库

管理仓库登录凭证(类似 docker login)。

# 登录到私有仓库
skopeo login --username user --password pass registry.example.com

# 注销
skopeo logout registry.example.com

8. standalone-sign/verify:签名与验证(实验性)

使用容器镜像签名(需配合容器策略文件)。

# 签名镜像
skopeo standalone-sign ./image-manifest.json registry.example.com/myimage:tag key.pem -o signature

# 验证签名
skopeo standalone-verify ./image-manifest.json registry.example.com/myimage:tag sha256:abc123 key.pub

关键选项

  • 认证:使用 --creds=user:password--authfile=/path/to/auth.json(如 Podman 的 ~/.docker/config.json)。
  • 传输协议:前缀指定源/目标类型(如 docker://, oci://, dir:, containers-storage:)。
  • 格式转换:通过 --format 指定目标格式(如 ociv2s2)。

典型应用场景

  • 镜像迁移:在 Docker Hub、私有仓库和本地存储之间复制镜像。
  • CI/CD 流水线:检查镜像元数据或同步镜像到生产环境。
  • 安全审计:验证镜像签名或删除不需要的镜像版本。

通过灵活的命令和轻量级设计,Skopeo 成为容器工具链中不可或缺的实用工具。

你可能感兴趣的:(Kubernetes,容器技术,云原生,kubernetes,经验分享,容器,devops,rancher,后端)