如何从标签

时间:2019-05-17 02:46:26

标签: docker containers

我想构建一个非常简单的系统(考虑一些Python行),将给定的docker标签(例如ubuntu:latest)转换为该标签当前指向的图像的SHA256摘要。

我知道,如果我们安装了Docker,我们可以简单地pull然后列出提取的映像的摘要。我在考虑是否可以在不实际拉动图像的情况下实现这一目标。

2 个答案:

答案 0 :(得分:0)

泊坞窗客户端未(似乎)显示此功能,因此您需要使用注册表客户端SDK。我使用的是Golang,无法找到易于使用且可在各个注册表中使用的SDK(除Docker Hub外,我还想使用Google Container Registry)。使用Python可能会更成功。

我一直在研究带有容器图像清单的项目,并写了一篇有关如何枚举清单和清单摘要的中型帖子。我希望它对您有用:

https://medium.com/google-cloud/adventures-w-docker-manifests-78f255d662ff

NB 与Docker Hub的Docker Registry HTTP v2 API的实现存在一些不一致之处。 FWIW,Google容器注册表(GCR)准确地实现了注册表API。我不在Google的GCR团队工作。

答案 1 :(得分:0)

您可以使用crane获取图像清单,然后使用skopeo计算摘要。

$ crane manifest ubuntu:18.04 | skopeo manifest-digest /dev/stdin
sha256:86510528ab9cd7b64209cbbe6946e094a6d10c6db21def64a93ebdd20011de1d

或者,您可以从skopeo inspect的输出中获得相同的摘要

$ skopeo inspect docker://ubuntu:18.04 | jq -r .Digest
sha256:86510528ab9cd7b64209cbbe6946e094a6d10c6db21def64a93ebdd20011de1d

或者,如果您希望在没有上述任何工具的情况下手动进行操作,请尝试以下操作:

$ curl -s -L \
  -H "Accept: application/vnd.docker.distribution.manifest.v2+json,application/vnd.docker.distribution.manifest.list.v2+json" \
  -H "Authorization: Bearer $(curl -s "https://auth.docker.io/token?service=registry.docker.io&scope=repository:library/ubuntu:pull" | jq -r .token)" \
  https://index.docker.io/v2/library/ubuntu/manifests/18.04 | sha256sum
86510528ab9cd7b64209cbbe6946e094a6d10c6db21def64a93ebdd20011de1d  -

实际上,一个HEAD请求足以获取清单哈希值

$ curl -s -L -I \
  -H "Accept: application/vnd.docker.distribution.manifest.v2+json,application/vnd.docker.distribution.manifest.list.v2+json" \
  -H "Authorization: Bearer $(curl -s "https://auth.docker.io/token?service=registry.docker.io&scope=repository:library/ubuntu:pull" | jq -r .token)" \
  https://index.docker.io/v2/library/ubuntu/manifests/18.04 | grep ^Docker-Content-Digest | awk '{print $2}'
sha256:86510528ab9cd7b64209cbbe6946e094a6d10c6db21def64a93ebdd20011de1d