Get latest Docker image creation date from registry

偶尔善良 提交于 2020-06-08 12:41:31

问题


How is it possible to get latest creation date of Docker image that exists at registry? Recently we have a problem that Docker image was not automatically pulled at some of our cluster slave servers and the project was running at the very outdated container environment. So am expecting to run a cron-script once per day to check that pulled Docker image is not 24 hours older than registry Docker image.


回答1:


The simplest way I know of is to write a simple script that uses the Docker Registry REST API and does basic manipulation of the results for your needs.

Here's some example code in Python 3 that would do this for a repo I work with:

import requests

repo_tag_url = 'https://hub.docker.com/v2/repositories/streamsets/datacollector/tags'
results = requests.get(repo_tag_url).json()['results']
for repo in results:
    print(repo['name'], repo['last_updated'])

Which, for my example, returns:

3.0.0.0-SNAPSHOT 2017-10-23T14:43:29.888877Z
latest 2017-10-05T23:05:03.636155Z
2.7.2.0 2017-10-05T22:50:53.269831Z
2.7.2.0-RC5 2017-10-05T19:34:19.523402Z
2.7.2.0-RC4 2017-10-05T02:05:52.522323Z
2.7.2.0-RC3 2017-10-04T00:08:02.929502Z
2.8.0.0-SNAPSHOT 2017-10-03T21:55:08.042479Z
2.7.2.0-RC2 2017-10-03T19:20:56.642686Z
2.7.2.0-RC21 2017-09-30T21:42:27.924190Z
2.7.2.0-RC1 2017-09-30T17:34:14.409320Z



回答2:


Something like Docker history?

From the doc:

Description: Show the history of an image

Usage: docker history [OPTIONS] IMAGE

For example:

 $ docker history imageName

And using the --format option:

$ docker images --format "{{.ID}}: {{.Created}} ago"

cc1b61406712: 2 weeks ago
<missing>: 2 weeks ago
<missing>: 2 weeks ago
<missing>: 2 weeks ago
<missing>: 2 weeks ago
<missing>: 3 weeks ago
<missing>: 3 weeks ago
<missing>: 3 weeks ago



回答3:


Below python3 code and Docker registry REST API worked for me.

import requests
import json

image_tags = requests.get('http://my-private-registry.example.com:5000/v2/myrepo/tags/list')
latest = []
for tag in image_tags.json()['tags']:
    url = requests.get('http://my-private-registry.example.com:5000/v2/myrepo/manifests/'+tag)
    latest.append((tag, json.loads(url.json()['history'][0]['v1Compatibility']).get('created')))

# sort the list based upon created timestamp stored as the second element of the tuple
latest.sort(key=lambda x: x[1])

# return latest image tag from tuple
latest_image = latest[-1][0]



来源:https://stackoverflow.com/questions/46892589/get-latest-docker-image-creation-date-from-registry

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!