我正在 Coreos 服务器上的 docker 容器中运行 react 应用程序。假设它是从 https://hub.docker.com/r/myimages/myapp
的 dockerhub 中提取的。
现在我想定期检查应用容器的 dockerhub 镜像是否已更新,看看我在本地运行的镜像是否落后。
与远程镜像相比,检查本地 docker 镜像是否已过时的最有效方法是什么?到目前为止,我发现的所有解决方案都是 bash 脚本或插入更新的外部服务。我想找到一个尽可能原生于 docker 的解决方案,并且希望避免从其他地方推送通知(以提醒服务器有更新的图像)。
最佳答案
您可以查询注册表 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
您可以将该 ETag 或 Docker-Content-Digest header 与您之前提取的镜像上的注册表引用进行比较:
$ 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
我还一直在研究一些 Go API 和 CLI,以便与更多您可能需要通过不同类型授权的注册中心合作。该项目位于 regclient/regclient并包含一个 regctl
命令。
$ regctl image digest --list busybox:latest
sha256:d366a4665ab44f0648d7a00ae3fae139d55e32f9712c67accd604bb55df9d05a
关于bash - 如何检查我的本地 docker 镜像是否已过时,而无需从其他地方推送?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42137511/