一、通過 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
import json
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):
html = requests.get(url).text
results = json.loads(html)
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! ')