簡體   English   中英

如何在不從其他地方推送的情況下檢查我的本地 docker 圖像是否已過時?

[英]How do I check if my local docker image is outdated, without pushing from somewhere else?

我在 Coreos 服務器上的 docker 容器中運行一個 React 應用程序。 假設它是從https://hub.docker.com/r/myimages/myapp從 dockerhub 中提取的。

現在我想定期檢查應用程序容器的 dockerhub 圖像是否已更新,以查看我在本地運行的圖像是否落后。

與遠程圖像相比,檢查本地 docker 圖像是否過時的最有效方法是什么? 到目前為止,我找到的所有解決方案都是 bash 腳本或推送更新的外部服務。 我想找到一個盡可能原生於 docker 的解決方案,並且希望避免從其他地方推送通知(以提醒服務器更新圖像)。

如果您正在使用 Docker Hub,您可以使用 Webhook 通知 Docker 主機有關更新,並對此采取行動。

使用 webhook 將是執行此操作的“簡單”方法(我認為),否則您將不得不在 docker pull 中進行某種爬行,或者如@alebianco 所解釋的那樣比較一些哈希或構建/創建日期。

這是關於它的文檔: https : //docs.docker.com/docker-hub/webhooks/

您可以查詢鏡像摘要的注冊表 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 標頭與您之前提取的映像上的注冊表引用進行比較:

$ 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

有一個用於 Docker Hub 的API

您應該能夠獲取標簽列表,並從那里獲取清單詳細信息


編輯

我做了一些挖掘,看起來他們沒有公開任何類型的圖像校驗和,它是清單或組成它的層。

我發現的最接近的是創建日期......如果您試圖使某些東西遠程安全,我不建議使用它。

無論如何,您需要先獲得訪問令牌

curl "https://auth.docker.io/token?service=registry.docker.io&scope=repository:library/ubuntu:pull"

從響應中提取令牌,然后您可以加載圖像版本的清單

curl --header "Authorization: Bearer $TOKEN" https://index.docker.io/v2/library/ubuntu/manifests/latest

查看返回的 json 的歷史對象,你會發現一個created屬性。

然后你可以得到你的本地圖像創建日期

docker inspect --format "{{json .Created}}" ubuntu:latest

比較兩者並畏縮......

我用一個直接檢查 Dockerfile 或from字符串的爬蟲解決了這個問題。

我將所有內容都支持到一個 docker 鏡像中,該鏡像可以在docker hub 中找到。

我只是在我的 gitlab ci 管道中運行圖像。 如果基本圖像已過時,則會打印所有較新版本,以便您可以輕松選擇標簽。

鏈接: https : //hub.docker.com/r/olafnorge/docker-image-crawler/

Issue 30906 (Docker command to check if a local container image is outdated?) 剛剛提到(2023 年 1 月)

您可以嘗試使用Diun來完成這項任務。
它會檢查更新,然后可以通過多個提供商通知您。

D ocker I mage U pdate Notifier 是一個 CLI 應用程序,用 Go 編寫並作為單個可執行文件(和一個 Docker 圖像)交付,以在 Docker 圖像在 Docker 注冊表上更新時接收通知。

使用 Go,這可以通過跨 Go 支持的所有平台和架構的獨立二進制分發來完成。

請參閱Marco d'Aleo博客文章“ DIUN:Docker Image Update Notifier

Diun 支持很多服務,但對於這種項目,我通常選擇 Telegram。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM