如何在远程注册表上列出Docker映像的所有标记?

如何使用CLI(首选)或curl在远程Docker注册表上列出Docker映像的所有标签?

最好不要从远程注册表中提取所有版本。我只想列出标签。

364949 次浏览

Docker Registry API有一个列出所有标签端点。

看起来Tutum有一个类似的端点,以及通过tutum-cli访问的方法。

使用tutum-cli,尝试以下操作:

tutum tag list <uuid>

我已经设法让它使用卷曲工作:

curl -u <username>:<password> https://myrepo.example/v1/repositories/<username>/<image_name>/tags

注意image_name不应该包含用户详细信息等。例如,如果你正在推送名为myrepo.example/username/x的图像,那么image_name应该是x

更新:遗憾的是,这个解决方案将不再工作,因为Docker有弃用了v1 API

我从在这里得到答案。非常感谢!:)

只有一行脚本:(找到debian的所有标签)

wget -q https://registry.hub.docker.com/v1/repositories/debian/tags -O -  | sed -e 's/[][]//g' -e 's/"//g' -e 's/ //g' | tr '}' '\n'  | awk -F: '{print $3}'

< p >更新 谢谢@ degree的建议。 下面是shell脚本
#!/bin/bash


if [ $# -lt 1 ]
then
cat << HELP


dockertags  --  list all tags for a Docker image on a remote registry.


EXAMPLE:
- list all tags for ubuntu:
dockertags ubuntu


- list all php tags containing apache:
dockertags php apache


HELP
fi


image="$1"
tags=`wget -q https://registry.hub.docker.com/v1/repositories/${image}/tags -O -  | sed -e 's/[][]//g' -e 's/"//g' -e 's/ //g' | tr '}' '\n'  | awk -F: '{print $3}'`


if [ -n "$2" ]
then
tags=` echo "${tags}" | grep "$2" `
fi


echo "${tags}"
你可以在/usr/local/bin下创建一个新文件名dockertags(或者在你的.bashrc/.zshrc中添加一个PATH env),并把代码放进去。 然后添加可执行权限(chmod +x dockertags)

用法:

dockertags ubuntu ---& gt;列出ubuntu的所有标签

dockertags php apache——比;列出所有包含'apache'的PHP标签

从Docker Registry V2开始,简单的GET就足够了:

GET /v2/<name>/tags/list

更多信息请参见文档

如果是JSON解析工具,则可用jq

wget -q https://registry.hub.docker.com/v1/repositories/debian/tags -O - | \
jq -r '.[].name'

你也可以使用这个废料:

# vim /usr/sbin/docker-tags

,以下(按原样):

#!/bin/bash
im="$1"
[[ -z "$im" ]] && { echo -e '\e[31m[-]\e[39m Where is the image name ??' ; exit ; }
[[ -z "$(echo "$im"| grep -o '/')" ]] && { link="https://hub.docker.com/r/library/$im/tags/" ; } || { link="https://hub.docker.com/r/$im/tags/" ; }
resp="$(curl -sL "$link")"
err="$(echo "$resp" | grep -o 'Page Not Found')"
if [[ ! -z "$err" ]] ; then
echo -e "\e[31m[-]\e[39m No Image Found with name => [ \e[32m$im\e[39m ]"
exit
else
tags="$(echo "$resp"|sed  -e 's|}|\n|g' -e 's|{|\n|g'|grep '"result"'|sed -e 's|,|\n|g'|cut -d '[' -f2|cut -d ']' -f1|sed  '/"tags":/d'|sed -e 's|"||g')"
echo -e "\e[32m$tags\e[39m"
fi

让它可执行:

# chmod 755 /usr/sbin/docker-tags

最后试试:

$ docker-tags testexampleidontexist
[-] No Image Found with name => [ testexampleidontexist ]


$ docker search ubuntu


$ docker-tags teamrock/ubuntu
latest

[希望你知道$ &# before running any command]

curl -u <username>:<password> https://$your_registry/v2/$image_name/tags/list -s -o - | \
tr -d '{' | tr -d '}' | sed -e 's/[][]//g' -e 's/"//g' -e 's/ //g' | \
awk -F: '{print $3}' | sed -e 's/,/\n/g'

如果你的env没有'jq'你可以使用它,=)

如果你想使用docker注册表v2 API,它会按页列出标签。要列出一张图片的所有标签,你可能想在url中添加一个大的page_size参数。

curl -L -s 'https://registry.hub.docker.com/v2/repositories/library/centos/tags?page_size=1024'|jq '."results"[]["name"]'

从Docker Hub获取所有标签:这个命令使用命令行JSON处理器jq从Docker Hub Registry返回的JSON中选择标签名(引号被tr删除)。将图书馆替换为Docker Hub用户名,将debian替换为映像名:

curl -s 'https://registry.hub.docker.com/v2/repositories/library/debian/tags/' | jq -r '."results"[]["name"]'

参见CLI实用程序:https://www.npmjs.com/package/docker-browse

允许枚举标签和图像。

docker-browse tags <image>将列出图像的所有标签。例如docker-browse tags library/alpine

docker-browse images将列出注册表中的所有图像。index.docker.io当前不可用。

你可以将它连接到任何注册表,包括你的私有注册表,只要它支持Docker registry HTTP API v2

基于Yan Foto的答案(v2 api),我创建了一个简单的Python脚本列出给定图像的标签

用法:

./docker-registry-list.py alpine

输出:

{
"name": "library/alpine",
"tags": [
"2.6",
"2.7",
"3.1",
"3.2",
"3.3",
"3.4",
"3.5",
"3.6",
"3.7",
"edge",
"latest"
]
}

这是我为Windows写的Powershell脚本。处理v1和v2回购:

Get-DockerImageVersions.ps1:

param (
[Parameter (Mandatory=$true)]$ImageName,
[Parameter (Mandatory=$false)]$RegistryURL
)


if (!$RegistryURL)
{
$RegistryURL = "https://registry.hub.docker.com/v1/repositories"
}


$list = ""
if ($RegistryURL -like "*v2*")
{
$list = "/list"
}


$URL = "$RegistryURL/$ImageName/tags$list"


write-debug $URL
$resp = Invoke-WebRequest -UseBasicParsing $URL | ConvertFrom-Json


if ($RegistryURL -like "*v2*")
{
$tags = $resp | select tags
$tags.tags
} else {
$tags = $resp | select name
$tags.name
}

Docker V2 API需要一个带有适当声明的OAuth承载令牌。在我看来,官方文件在这个话题上相当模糊。为了让其他人不会经历我所经历的痛苦,我提供了下面的docker-tags函数。

docker-tags的最新版本可以在我的GitHubGist:“使用bash列出Docker图像标签”中找到。

docker-tags函数依赖于金桥。如果您正在使用JSON,那么您可能已经拥有它了。

#!/usr/bin/env bash
docker-tags() {
arr=("$@")


for item in "${arr[@]}";
do
tokenUri="https://auth.docker.io/token"
data=("service=registry.docker.io" "scope=repository:$item:pull")
token="$(curl --silent --get --data-urlencode ${data[0]} --data-urlencode ${data[1]} $tokenUri | jq --raw-output '.token')"
listUri="https://registry-1.docker.io/v2/$item/tags/list"
authz="Authorization: Bearer $token"
result="$(curl --silent --get -H "Accept: application/json" -H "Authorization: Bearer $token" $listUri | jq --raw-output '.')"
echo $result
done
}

例子

docker-tags "microsoft/nanoserver" "microsoft/dotnet" "library/mongo" "library/redis"

无可否认,docker-tags做了几个假设。具体来说,OAuth请求参数大多是硬编码的。更有野心的实现将向注册中心发出未经身份验证的请求,并从未经身份验证的响应中派生OAuth参数。

当我必须实现一个任务时,如果用户以某种方式键入错误的标记,那么我们必须给出寄存器中存在的repo(Docker repo)中的所有标记的列表。 所以我有代码在批处理脚本
<html>
<pre style="background-color:#bcbbbb;">
@echo off


docker login --username=xxxx --password=xxxx
docker pull %1:%2


IF NOT %ERRORLEVEL%==0 (
echo "Specified Version is Not Found "
echo "Available Version for this image is :"
for /f %%i in (' curl -s -H "Content-Type:application/json" -X POST -d "{\"username\":\"user\",\"password\":\"password\"}" https://hub.docker.com/v2/users/login ^|jq -r .token ') do set TOKEN=%%i
curl -sH "Authorization: JWT %TOKEN%" "https://hub.docker.com/v2/repositories/%1/tags/" | jq .results[].name
)
</pre>
</html>

因此,在这里,我们可以为批处理文件提供参数,如:

Dockerfile java版本7

查看浏览器中所有可用的标签。

https://registry.hub.docker.com/v1/repositories/<username>/<image_name>/tags

例如https://hub.docker.com/r/localstack/localstack/tags

或者,你可以使用这个端点得到一个json响应:

https://registry.hub.docker.com/v1/repositories/localstack/localstack/tags

你可以通过在终端上运行来实现:

curl -L -s 'https://registry.hub.docker.com/v2/repositories/library/mysql/tags/' | jq . | grep name

此外,如果你没有jq,你必须安装它

sudo apt-get install jq

如果人们想从RedHat注册表https://registry.redhat.io/v2读取标签,那么步骤是:

# example nodejs-12 image
IMAGE_STREAM=nodejs-12
REDHAT_REGISTRY_API="https://registry.redhat.io/v2/rhel8/$IMAGE_STREAM"
# Get an oAuth token based on a service account username and password https://access.redhat.com/articles/3560571
TOKEN=$(curl --silent -u "$REGISTRY_USER":"$REGISTRY_PASSWORD" "https://sso.redhat.com/auth/realms/rhcc/protocol/redhat-docker-v2/auth?service=docker-registry&client_id=curl&scope=repository:rhel:pull" |  jq --raw-output '.token')
# Grab the tags
wget -q --header="Accept: application/json" --header="Authorization: Bearer $TOKEN" -O - "$REDHAT_REGISTRY_API/tags/list" | jq -r '."tags"[]'

如果你想比较本地openshift注册表和上游注册表中的内容,那么这里是完整的脚本

在powershell 5.1中,我有一个简单的list_docker_image_tags.ps1脚本,像这样:

[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string]
$image
)


$url = "https://registry.hub.docker.com/v1/repositories/{0}/tags" -f $image
Invoke-WebRequest $url  | ConvertFrom-Json | Write-Output

然后我可以grep 4.7标签,像这样:

./list_docker_image_tags.ps1 microsoft/dotnet-framework | ?{ $_.name -match "4.7" }

你可以列出所有带有skopeojq的标签,以便通过cli解析json。

skopeo --override-os linux inspect docker://httpd | jq '.RepoTags'
[
"2-alpine",
"2.2-alpine",
"2.2.29",
"2.2.31-alpine",
"2.2.31",
"2.2.32-alpine",
"2.2.32",
"2.2.34-alpine",
"2.2.34",
"2.2",
"2.4-alpine",
"2.4.10",
"2.4.12",
"2.4.16",
"2.4.17",
"2.4.18",
"2.4.20",
"2.4.23-alpine",
"2.4.23",
"2.4.25-alpine",
"2.4.25",
"2.4.27-alpine",
"2.4.27",
"2.4.28-alpine",
"2.4.28",
"2.4.29-alpine",
"2.4.29",
"2.4.32-alpine",
"2.4.32",
"2.4.33-alpine",
"2.4.33",
"2.4.34-alpine",
"2.4.34",
"2.4.35-alpine",
"2.4.35",
"2.4.37-alpine",
"2.4.37",
"2.4.38-alpine",
"2.4.38",
"2.4.39-alpine",
"2.4.39",
"2.4.41-alpine",
"2.4.41",
"2.4.43-alpine",
"2.4.43",
"2.4",
"2",
"alpine",
"latest"
]

对于外部注册中心:

skopeo --override-os linux inspect --creds username:password docker://<registry-url>/<repo>/<image> | jq '.RepoTags'

注意:--override-os linux只在不是运行在linux主机上时才需要。例如,如果你在MacOS上,你会有更好的结果。

你可以使用:

skopeo inspect docker://<REMOTE_REGISTRY> --authfile <PULL_SECRET> | jq .RepoTags

这是一个适用于注册表v2的答案。

如果你的机器上安装了jqcurl:

curl https://registry.hub.docker.com/v2/repositories/$REPOSITORY/tags?page_size=10000 | jq '.results[] | { name: .name, architectures: ([ (.images[] | if .variant? then .os + "/" + .architecture + .variant? else .os + "/" + .architecture end) ] | join(", ")) }'

例如,为curlimages/curl存储库运行此命令会生成:

{
"name": "latest",
"architectures": "linux/ppc64le, linux/s390x, linux/arm64, linux/386, linux/armv7, linux/amd64"
}
{
"name": "7.78.0",
"architectures": "linux/armv7, linux/arm64, linux/386, linux/s390x, linux/ppc64le, linux/amd64"
}
{
"name": "7.77.0",
"architectures": "linux/ppc64le, linux/arm64, linux/s390x, linux/armv7, linux/386, linux/amd64"
}
{
"name": "7.76.1",
"architectures": "linux/386, linux/arm64, linux/armv7, linux/ppc64le, linux/s390x, linux/amd64"
}
{
"name": "7.76.0",
"architectures": "linux/armv7, linux/386, linux/s390x, linux/amd64, linux/ppc64le, linux/arm64"
}
{
"name": "7.75.0",
"architectures": "linux/armv7, linux/ppc64le, linux/386, linux/amd64, linux/arm64, linux/s390x"
}
{
"name": "7.74.0",
"architectures": "linux/armv7, linux/386, linux/amd64, linux/ppc64le, linux/s390x, linux/arm64"
}
{
"name": "7.73.0",
"architectures": "linux/arm64, linux/armv7, linux/s390x, linux/ppc64le, linux/amd64, linux/386"
}
{
"name": "7.72.0",
"architectures": "linux/s390x, linux/amd64, linux/arm64, linux/386, linux/ppc64le, linux/armv7"
}
{
"name": "7.71.1",
"architectures": "linux/s390x, linux/arm64, linux/ppc64le, linux/amd64, linux/386, linux/armv7"
}
{
"name": "7.71.0",
"architectures": "linux/arm64, linux/ppc64le, linux/386, linux/s390x, linux/amd64, linux/armv7"
}
{
"name": "7.70.0",
"architectures": "linux/386, linux/arm64, linux/s390x, linux/amd64, linux/ppc64le, linux/armv7"
}
{
"name": "7.69.1",
"architectures": "linux/amd64"
}
{
"name": "7.69.0",
"architectures": "linux/amd64"
}
{
"name": "7.68.0",
"architectures": "linux/amd64"
}
{
"name": "7.67.0",
"architectures": "linux/amd64"
}
{
"name": "7.66.0",
"architectures": "linux/amd64"
}
{
"name": "7.65.3",
"architectures": "linux/amd64"
}

<强>编辑: 回答这个问题:

如何使用CLI(首选)或curl在远程Docker注册表上列出Docker映像的所有标签?

最好不要从远程注册表中提取所有版本。我只想列出标签。

要获取图像的所有标签,您可以使用&;curl&;来获得你想要的特定图像,并将输出管道到"金桥"来提取信息。

curl -L -s 'https://registry.hub.docker.com/v2/repositories/library/python/tags?page_size=1024'|jq  '.results[]["name"]'

输出(截断而不是完整的列表):

"3.9-windowsservercore"
"alpine3.14"
"alpine3.13"
"alpine"
"3.9.8-alpine3.14"
"3.9.8-alpine3.13"
"3.9.8-alpine"

此外,如果您需要来自注册表的其他信息,您可以像这样访问其他字段信息。

这个命令将为您提供标签和图像大小,这可能也是有用的。

curl -L -s 'https://registry.hub.docker.com/v2/repositories/library/python/tags?page_size=1024'|jq  '.results[] as $results | ($results["name"] + " - " + ($results["full_size"] | tostring))'

输出(截断而不是完整的列表):

 "3.9-windowsservercore - 2241040278"
"alpine3.14 - 17565702"
"alpine3.13 - 17556181"
"alpine - 17565702"
"3.9.8-alpine3.14 -17362557"
"3.9.8-alpine3.13 - 17353629"
"3.9.8-alpine - 17362557"
在@AlexForbes的回答的基础上,我改进了api v2 docker-registry-list.py来支持 -存储库名称中的斜杠(例如curlimages/curl)和
- private repos(用户名和密码认证)

https://github.com/axil/docker-registry-list

用法:

./docker-registry-list.py -u dockerid -p password dockerid/myrepo

输出:

{
"name": "dockerid/myrepo",
"tags": [
"1.0"
]
}

正在寻找一个java sdk,我可以用来击中Docker V2 API,但找不到一个。对于任何可能发现它有用的人来说,这里是Repo: https://github.com/fern-api/docker-registry-api

应该可以在其他语言生成,随时打开一个问题上的回购!

到目前为止给出的答案有很多重复。 大多数人都没有考虑到GitHub API(至少v2)不会一次返回超过100个结果,即使你要求更多。我在请求php.

的标记时注意到这一点

下面的脚本可以解决这个问题。

#!/bin/sh


# list the tags on Docker Hub for the given image(s)
# thank you, https://stackoverflow.com/questions/28320134/how-can-i-list-all-tags-for-a-docker-image-on-a-remote-registry


TagsFor()
{
curl -L -s 'https://registry.hub.docker.com/v2/repositories/library/'$1'/tags?page='$2'&page_size'=$3
}


for i in "$@"
do
TagsFor "$i" 1 10 |
jq -r .count |
while read nr_of_tags
do
nr_of_pages=`expr $nr_of_tags / 100`
seq 1 $nr_of_pages |
while read p
do
TagsFor "$i" "$p" 100 |
jq -r '.results[] | .name'
done
done
done

我只是运行脚本;它检索了7200个php标签。据我所知,它可能会达到另一个API限制,但7200 >>One hundred.

这是一个脚本,列出所有2或3位数字的标签。 你可以直接在github上获得代码 https://github.com/youssefalaoui/dockerhub-tools/blob/main/dockerhub-list-tags.sh < / p >

dockerhub_list_tags()
{
#local LOCAL_IMAGE LOCAL_GET_TWO_DIGITS_VERSIONS
     

LOCAL_IMAGE=${1:-null}
LOCAL_GET_TWO_DIGITS_VERSIONS=${2:-true}




if [[ $LOCAL_IMAGE == "" || $LOCAL_IMAGE == null ]]
then
printf "Image name is required: %s" ${FUNCNAME[0]};
exit 1;
fi


#[[ $LOCAL_IMAGE == "" || $LOCAL_IMAGE == null ]] && printf "Image name is required: %s" ${FUNCNAME[0]}; exit 1;


echo "Listing tags from docker hub for your image '$LOCAL_IMAGE'"
    

# Check if 2 digits format is requested, otherwise, show it in normal format
    

if [[ "$LOCAL_GET_TWO_DIGITS_VERSIONS" == true ]]; then
DOCKERHUB_LIST_TAGS=($(curl -L -s "https://registry.hub.docker.com/v2/repositories/$LOCAL_IMAGE/tags?page_size=1024"|jq '."results"[]["name"]' | sed 's/"//g' | sed 's/\.[^.]*$//'))
else
DOCKERHUB_LIST_TAGS=($(curl -L -s "https://registry.hub.docker.com/v2/repositories/$LOCAL_IMAGE/tags?page_size=1024"|jq '."results"[]["name"]' | sed 's/"//g'))
fi


for TAG in ${DOCKERHUB_LIST_TAGS[@]}
do
echo $TAG
done
}




# Test example
dockerhub_list_tags "library/nginx" false

我的贡献:

  • Shell脚本
  • 尽可能的简短
  • 需要curljq
  • 使用Docker v2 REST API
  • 使用REST API分页返回所有标记

例子:

$ docker-tags prantlf/chromedriver-headless
latest
102
93
86

脚本内容:

#!/bin/sh


image=$1
if [ "$image" == "" ]; then
echo "Usage:
docker-tags <image>


Example:
docker-tags library/ubuntu"
exit 0
fi


page_size=100
page_index=0
while true; do
page_index=$((page_index+1))
results=`curl -L -s "https://registry.hub.docker.com/v2/repositories/$image/tags?page=$page_index&page_size=$page_size" | jq -r 'select(.results != null) | .results[]["name"]'`
if [ $? != 0 ] || [ "$results" == "" ]; then
break
fi
echo "$results"
done