如何使用Docker Registry HTTP API V2获取Docker注册表中所有存储库的列表? [英] How can I use Docker Registry HTTP API V2 to obtain a list of all repositories in a docker registry?
问题描述
我与之合作的外部组织允许我访问私有(受身份验证令牌保护的)Docker注册表,最终我希望能够使用Docker的HTTP API V2查询此注册表,以获得列表。注册表中所有可用的存储库和/或映像。
An external organization that I work with has given me access to a private (auth token protected) docker registry, and eventually I would like to be able to query this registry, using docker's HTTP API V2, in order to obtain a list of all the repositories and/or images available in the registry.
但是在我这样做之前,我首先想获得一些基本的实践来构造这些类型的API在公共注册表上查询,例如 Docker Hub 。因此,我继续前进,并在Docker Hub上使用用户名和密码注册了自己查阅了API V2文档,该文档指出可以请求 API版本检查为:
But before I do that, I'd first like to get some basic practice with constructing these types of API queries on a public registry such as Docker Hub. So I've gone ahead and registered myself with a username and password on Docker Hub, and also consulted the API V2 documentation, which states that one may request an API version check as:
GET /v2/
或请求存储库列表:
GET /v2/_catalog
使用curl以及我用来注册Docker Hub帐户的用户名和密码,我尝试在命令行上构造GET请求:
Using curl, together with the username and password that I used in order to register my Docker Hub account, I attempt to construct a GET request at the command line:
stachyra> curl -u stachyra:<my_password> -X GET https://index.docker.io/v2/
{"errors":[{"code":"UNAUTHORIZED","message":"authentication required","detail":null}]}
stachyra> curl -u stachyra:<my_password> -X GET https://index.docker.io/v2/_catalog
{"errors":[{"code":"UNAUTHORIZED","message":"authentication required","detail":[{"Type":"registry","Class":"","Name":"catalog","Action":"*"}]}]}
当然,我用实际的帐户密码代替了< my_password>
。
where of course, in place of <my_password>
, I substituted my actual account password.
我一直希望从该查询中获得的响应是一条巨大的json消息,其中列出了数千个存储库名称,但该API似乎拒绝了我的Docker Hub凭据。
The response that I had been expecting from this query was a giant json message, listing thousands of repository names, but instead it appears that the API is rejecting my Docker Hub credentials.
问题1:我是否具有正确的URL( index.docker.io
)码头中心注册表? (我首先是根据命令行工具 docker info
返回的状态信息做出此假设的,所以我有充分的理由认为它是正确的。)
Question 1: Do I even have the correct URL (index.docker.io
) for the docker hub registry? (I made this assumption in the first place based upon the status information returned by the command line tool docker info
, so I have good reason to think it's correct.)
问题2:假定我具有注册表服务本身的正确URL,为什么我的查询返回 UNAUTHORIZED错误代码?当我尝试通过网络在hub.docker.com上登录时,我的帐户凭据可以正常工作,所以两种情况有什么区别?
Question 2: Assuming I have the correct URL for the registry service itself, why does my query return an "UNAUTHORIZED" error code? My account credentials work just fine when I attempt to login via the web at hub.docker.com, so what's the difference between the two cases?
推荐答案
这是从注册表读取存储库的示例程序。我将它用作Docker Hub的学习辅助工具。
Here is an example program to read repositories from a registry. I used it as a learning aid with Docker Hub.
#!/bin/bash
set -e
# set username and password
UNAME="username"
UPASS="password"
# get token to be able to talk to Docker Hub
TOKEN=$(curl -s -H "Content-Type: application/json" -X POST -d '{"username": "'${UNAME}'", "password": "'${UPASS}'"}' https://hub.docker.com/v2/users/login/ | jq -r .token)
# get list of repos for that user account
REPO_LIST=$(curl -s -H "Authorization: JWT ${TOKEN}"
https://hub.docker.com/v2/repositories/${UNAME}/?page_size=10000 | jq -r '.results|.[]|.name')
# build a list of all images & tags
for i in ${REPO_LIST}
do
# get tags for repo
IMAGE_TAGS=$(curl -s -H "Authorization: JWT ${TOKEN}"
https://hub.docker.com/v2/repositories/${UNAME}/${i}/tags/?page_size=10000 | jq -r '.results|.[]|.name')
# build a list of images from tags
for j in ${IMAGE_TAGS}
do
# add each tag to list
FULL_IMAGE_LIST="${FULL_IMAGE_LIST} ${UNAME}/${i}:${j}"
done
done
# output list of all docker images
for i in ${FULL_IMAGE_LIST}
do
echo ${i}
done
(这来自Docker站点上的文章描述了如何使用API。)
(this comes from an article on Docker site that describes how to use the API.)
本质上...
- 获取令牌
- 将令牌作为标头
传递:JWT< token>
与您进行的任何API调用 - api ca您要用来列出存储库的ll是
https://hub.docker.com/v2/repositories/<username>/
- get a token
- pass the token as a header
Authorization: JWT <token>
with any API calls you make - the api call you want to use to list repositories is
https://hub.docker.com/v2/repositories/<username>/
这篇关于如何使用Docker Registry HTTP API V2获取Docker注册表中所有存储库的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!