如何在不从其他地方推送的情况下检查我的本地docker镜像是否过时?

17 bash systemd docker coreos

我正在Coreos服务器上的docker容器中运行react应用程序.让我们说它是从dockerhub拉出来的https://hub.docker.com/r/myimages/myapp.

现在我想定期检查app容器的dockerhub映像是否已更新,以查看我在本地运行的映像是否落后.

与远程图像相比,检查本地泊坞窗图像是否过时的最有效方法是什么?到目前为止,我发现的所有解决方案都是bash脚本或推动更新的外部服务.我想找到一个尽可能与docker原生的解决方案,并且希望不要从其他地方推送通知(以警告服务器更新图像).

BMi*_*tch 11

您可以查询注册表 API 来获取图像摘要,并将其与您提取的图像摘要进行比较。

$ cat digest-v2.sh
#!/bin/sh

ref="${1:-library/ubuntu:latest}"
repo="${ref%:*}"
tag="${ref##*:}"
acceptM="application/vnd.docker.distribution.manifest.v2+json"
acceptML="application/vnd.docker.distribution.manifest.list.v2+json"
token=$(curl -s "https://auth.docker.io/token?service=registry.docker.io&scope=repository:${repo}:pull" \
        | jq -r '.token')
curl -H "Accept: ${acceptM}" \
     -H "Accept: ${acceptML}" \
     -H "Authorization: Bearer $token" \
     -I -s "https://registry-1.docker.io/v2/${repo}/manifests/${tag}"

$ ./digest-v2.sh library/busybox:latest
HTTP/1.1 200 OK
Content-Length: 2080
Content-Type: application/vnd.docker.distribution.manifest.list.v2+json
Docker-Content-Digest: sha256:d366a4665ab44f0648d7a00ae3fae139d55e32f9712c67accd604bb55df9d05a
Docker-Distribution-Api-Version: registry/2.0
Etag: "sha256:d366a4665ab44f0648d7a00ae3fae139d55e32f9712c67accd604bb55df9d05a"
Date: Sun, 11 Oct 2020 21:04:59 GMT
Strict-Transport-Security: max-age=31536000
Run Code Online (Sandbox Code Playgroud)

您可以将该 ETag 或 Docker-Content-Digest 标头与您之前提取的映像上的注册表引用进行比较:

$ docker image inspect busybox:latest --format '{{json .RepoDigests}}' | jq .
[
  "busybox@sha256:d366a4665ab44f0648d7a00ae3fae139d55e32f9712c67accd604bb55df9d05a"
]

$ docker image pull busybox:latest
latest: Pulling from library/busybox
Digest: sha256:d366a4665ab44f0648d7a00ae3fae139d55e32f9712c67accd604bb55df9d05a
Status: Image is up to date for busybox:latest
docker.io/library/busybox:latest
Run Code Online (Sandbox Code Playgroud)

我还一直在开发一些 Go API 和 CLI,以便与更多的注册中心合作,在这些注册中心中您可能需要通过不同类型的授权。该项目位于regclient/regclient并包含一个regctl命令。

$ regctl image digest --list busybox:latest
sha256:d366a4665ab44f0648d7a00ae3fae139d55e32f9712c67accd604bb55df9d05a
Run Code Online (Sandbox Code Playgroud)


Luc*_*reu 6

如果您使用的是Docker Hub,则可以使用Webhook通知docker主机有关更新的信息,并对其采取措施.

使用webhook将是"简单"的方式(我认为),否则你将不得不在docker pull中进行某种爬行或者@alebianco比较一些散列或构建/创建日期.

以下是有关它的文档:https://docs.docker.com/docker-hub/webhooks/


ale*_*nco 4

Docker Hub有一个可用的 API

您应该能够获取标签列表,并从那里获取清单详细信息


编辑

我做了一些挖掘,看起来他们没有公开任何类型的图像校验和,它是明显的或组成它的层。

我发现的最接近的是创建日期......如果您试图使某些东西远程安全,我不建议使用它。

无论如何,您需要先获得访问令牌

curl "https://auth.docker.io/token?service=registry.docker.io&scope=repository:library/ubuntu:pull"
Run Code Online (Sandbox Code Playgroud)

从响应中提取令牌,然后您可以加载图像版本的清单

curl --header "Authorization: Bearer $TOKEN" https://index.docker.io/v2/library/ubuntu/manifests/latest
Run Code Online (Sandbox Code Playgroud)

查看返回的 json 的历史对象,您会发现一个创建的属性。

然后你可以得到你的本地图像创建日期

docker inspect --format "{{json .Created}}" ubuntu:latest
Run Code Online (Sandbox Code Playgroud)

比较一下两者并畏缩......