从注册表中获取最新的 Docker 映像创建日期
Get latest Docker image creation date from registry
如何获取注册表中存在的 Docker 图像的最新创建日期?最近我们遇到了一个问题,Docker 图像没有在我们的一些集群从属服务器上自动拉取,并且项目在非常过时的容器环境中 运行ning。所以我希望每天 运行 一个 cron-script 来检查拉取的 Docker 图像是否比注册表 Docker 图像早 24 小时。
类似于 Docker history?
来自文档:
Description: Show the history of an image
Usage: docker history [OPTIONS] IMAGE
例如:
$ docker history imageName
并使用 --format 选项:
$ 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
据我所知,最简单的方法是编写一个简单的脚本,使用 Docker Registry REST API 并根据您的需要对结果进行基本操作。
这是 Python 3 中的一些示例代码,可以为我使用的存储库执行此操作:
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'])
以我为例,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
下面的 python3 代码和 Docker 注册表 REST API 对我有用。
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]
如何获取注册表中存在的 Docker 图像的最新创建日期?最近我们遇到了一个问题,Docker 图像没有在我们的一些集群从属服务器上自动拉取,并且项目在非常过时的容器环境中 运行ning。所以我希望每天 运行 一个 cron-script 来检查拉取的 Docker 图像是否比注册表 Docker 图像早 24 小时。
类似于 Docker history?
来自文档:
Description: Show the history of an image
Usage: docker history [OPTIONS] IMAGE
例如:
$ docker history imageName
并使用 --format 选项:
$ 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
据我所知,最简单的方法是编写一个简单的脚本,使用 Docker Registry REST API 并根据您的需要对结果进行基本操作。
这是 Python 3 中的一些示例代码,可以为我使用的存储库执行此操作:
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'])
以我为例,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
下面的 python3 代码和 Docker 注册表 REST API 对我有用。
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]