Docker 查看远端仓库的标签和镜像大小
一、通过 registry v1 api 接口此接口只能获取标签,不能获取镜像大小# 查询 URL 为https://registry.hub.docker.com/v1/repositories/【镜像名】/tags# 如 centos 的所有镜像标签https://registry.hub.docker.com/v1/repositories/centos/tags# 用...
·
一、通过 registry v1 api 接口
此接口只能获取标签,不能获取镜像大小
# 查询 URL 为
https://registry.hub.docker.com/v1/repositories/【镜像名】/tags
# 如 centos 的所有镜像标签
https://registry.hub.docker.com/v1/repositories/centos/tags
# 用户 babygod 仓库中 centos-ssh 标签
https://registry.hub.docker.com/v1/repositories/babygod/centos-ssh/tags
接口返回的数据为 json 数据,可以通过 jq
等工具进行格式转换
[root@test-02 ~] curl -s https://registry.hub.docker.com/v1/repositories/babygod/centos-ssh/tags | jq .
[
{
"layer": "",
"name": "latest"
},
{
"layer": "",
"name": "v1"
}
]
[root@test-02 ~] curl -s https://registry.hub.docker.com/v1/repositories/babygod/centos-ssh/tags | jq -r .[].name
latest
v1
通过脚本来获取标签
[root@test-02 ~]# cat dockertags.sh
#!/bin/bash
#
usage() {
cat << HELP
$0 -- list all tags for a Docker image on a remote registry.
EXAMPLE:
- list all tags for centos:
$0 centos
- list all php tags containing apache:
$0 php apache
HELP
}
if [ $# -lt 1 ]; then
usage
exit
fi
image="$1"
tags=$(curl -s https://registry.hub.docker.com/v1/repositories/${image}/tags | jq -r .[].name)
if [ -n "$2" ]
then
tags=$(echo "${tags}" | fgrep "$2")
fi
echo "${tags}"
# 用法如下
[root@test-02 ~]# ./dockertags.sh babygod/centos-ssh
latest
v1
Ref
二、通过 docker hub v2 api 接口
此接口获取的信息比较多,包括 tag name,full_size 等等
# 查询 URL 为 ( 将 page_size 设置一个比较大的值 )
https://hub.docker.com/v2/repositories/library/【镜像名】/tags/?page_size=10000
# 如 centos
https://hub.docker.com/v2/repositories/library/centos/tags/?page_size=100000
# 用户 babygod 仓库中 centos-ssh
https://hub.docker.com/v2/repositories/babygod/centos-ssh/tags/?page_size=1000
# 注意:默认是 library,如果某个用户的话,就需要把 library 改成相应的用户名,如上面把 library 改成 babyggod
接口返回的数据同样为 json 数据,可以通过 jq
等工具进行格式转换
[root@test-02 ~]# curl -s https://hub.docker.com/v2/repositories/babygod/centos-ssh/tags/?page_size=10000 | jq .
# 获取标签名
[root@test-02 ~]# curl -s https://hub.docker.com/v2/repositories/babygod/centos-ssh/tags/?page_size=10000 | jq -r .results[].name
latest
v1
# 获取镜像大小
[root@test-02 ~]# curl -s https://hub.docker.com/v2/repositories/babygod/centos-ssh/tags/?page_size=10000 | jq -r .results[].full_size
82550131
82287724
# 获取对应标签和大小
[root@test-02 ~]# curl -s https://hub.docker.com/v2/repositories/babygod/centos-ssh/tags/?page_size=10000 | jq -r '.results[]|[.name,.full_size|tostring]|join(": ")'
latest: 82550131
v1: 82287724
通过脚本来获取标签和大小
[root@test-02 ~]# cat dockertags_size.sh
#!/bin/bash
#
usage() {
cat << HELP
$0 -- list all tags and full_size for a Docker image on a remote registry.
EXAMPLE:
- list all tags and full_size for centos:
$0 centos
HELP
}
if [ $# -lt 1 ]; then
usage
exit
fi
fgrep -q '/' <<< "$1" && {
image="$1"
} || {
image="library/$1"
}
tags_size=$(curl -s https://hub.docker.com/v2/repositories/${image}/tags/?page_size=10000 | \
jq -r '.results[]|[.name,.full_size|tostring]|join(" ")')
echo "${tags_size}" | awk '{printf "%-20s %.2fMB\n",$1,$2/1000000}'
# 用法如下
[root@test-02 ~]# ./dockertags_size.sh babygod/centos-ssh
latest 82.55MB
v1 82.29MB
python 脚本
#!/usr/bin/env python
from sys import argv
import requests
def help_text():
text = '''
{0} -- list all tags and full_size for a Docker image on a remote registry.
EXAMPLE:
- list all tags and full_size for centos:
{0} centos
'''.format(argv[0])
print(text)
def get_json(url):
results = requests.get(url).json()
return results
def get_results():
results = get_json(url)
for i in results["results"]:
print("%-20s %.2fMB" % (i["name"], i["full_size"] / 1000000))
next_url = results["next"]
while next_url:
results = get_json(next_url)
for n in results["results"]:
print("%-20s %.2fMB" % (n["name"], n["full_size"] / 1000000))
next_url = results["next"]
def check_if_in_any(string, iterable):
return any(map(lambda item: item == string, iterable))
if __name__ == '__main__':
if len(argv) < 2 or any([check_if_in_any(x, ['-h', '--help']) for x in argv]):
help_text()
exit(1)
image = argv[1] if '/' in argv[1] else 'library/' + argv[1]
url = "https://hub.docker.com/v2/repositories/" + image + "/tags/?page_size=100"
try:
get_results()
except KeyError:
exit('Please enter the correct image name! ')
Ref
更多推荐
已为社区贡献1条内容
所有评论(0)