bash - 如何检查我的本地 docker 镜像是否已过时,而无需从其他地方推送?

标签 bash docker systemd coreos

我正在 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/

相关文章:

regex - 在 Bash 脚本中返回正则表达式匹配,而不是替换它

xml - 如何在 Bash 中解析 XML?

Windows 10 上守护进程的 Docker Mount Project 错误响应

docker - 使用 Docker 自动构建的多镜像

hadoop - CoreOS & HDFS - 在 Linux 容器/Docker 中运行分布式文件系统

linux - 在 bash 中扩展 {..} 内的变量?

bash - Bash 中的动态变量名称

bash - 在 systemd 引导期间访问标准输入

linux - 从 systemd 启动主进程时无法分 ionic 进程

ubuntu - 在 AWS EC2 Ubuntu 中设置 kafka.service 和 zookeeper.service