本节我们介绍一下docker registry HTTP API V2,然后给出几个调用API的实例。在这里为了方便,我们首先dump下docker的官方文档:
# docker pull docs/docker.github.io:latest
# docker run -itd -p 80:4000 docs/docker.github.io:latest
1. docker镜像仓库HTTP API V2
Method |
Path |
Entity |
Description |
GET |
/v2/ |
Base |
|
GET |
/v2//tags/list |
Tags |
|
GET |
/v2//manifests/ |
Manifest |
|
2. htpasswd认证条件下API测试
这里为了测试方便,我们暂时去掉https,只在htpasswd认证条件下对API进行测试。如下是我们的启动脚本:
首先我们测试一下/v2
:
# curl -X GET http://10.17.153.196:5000/v2 -k -IL
HTTP/1.1 301 Moved Permanently
Docker-Distribution-Api-Version: registry/2.0
Location: /v2/
Date: Mon, 26 Mar 2018 11:33:38 GMT
Content-Length: 39
Content-Type: text/html; charset=utf-8
HTTP/1.1 401 Unauthorized
Content-Type: application/json; charset=utf-8
Docker-Distribution-Api-Version: registry/2.0
Www-Authenticate: Basic realm="Registry Realm"
X-Content-Type-Options: nosniff
Date: Mon, 26 Mar 2018 11:33:38 GMT
Content-Length: 87
我们发现提示Unauthorized
。查看官方文档,我们只找到基于auth token
方式的认证。这里我们通过执行docker login命令,采用tcpdump抓取到如下包:
GET /v2/ HTTP/1.1
Host: 10.17.153.196:5000
User-Agent: docker/17.12.1-ce go/go1.9.4 git-commit/7390fc6 kernel/3.10.0-514.el7.x86_64 os/linux arch/amd64 UpstreamClient(Docker-Client/17.12.1-ce \(linux\))
Authorization: Basic YWRtaW46TWlkZWFAMTIz
Accept-Encoding: gzip
Connection: close
从上面我们可以看到在发送到docker registry中的请求中有Authorization: Basic YWRtaW46TWlkZWFAMTIz
头。我们猜想YWRtaW46TWlkZWFAMTIz
正是我们输入的用户名密码。但是该用户名密码到底是如何变成该字符串的呢?
这里我们分析docker官方源代码docker/registry/auth.go,发现有如下函数:
再跟踪v2AuthHTTPClient()函数,最后会在vendor/github.com/docker/distribution/registry/client/auth/session.go中找到如下:
其实就是对username + ":" + password
做base64。
有了上面的基础我们再对docker registry的HTTP API做一些测试:
1) 获得docker registry API版本
# curl -X GET -H "Authorization: Basic YWRtaW46TWlkZWFAMTIz" http://10.17.153.196:5000/v2 -k -IL
HTTP/1.1 301 Moved Permanently
Docker-Distribution-Api-Version: registry/2.0
Location: /v2/
Date: Mon, 26 Mar 2018 11:56:38 GMT
Content-Length: 39
Content-Type: text/html; charset=utf-8
HTTP/1.1 200 OK
Content-Length: 2
Content-Type: application/json; charset=utf-8
Docker-Distribution-Api-Version: registry/2.0
X-Content-Type-Options: nosniff
Date: Mon, 26 Mar 2018 11:56:38 GMT
上面我们看到返回200 OK
,测试成功,说明API版本是v2
2) /v2/_catalog获得当前仓库中的镜像
# curl -X GET -H "Authorization: Basic YWRtaW46TWlkZWFAMTIz" -H "Accept: application/json" http://10.17.153.196:5000/v2/_catalog
{"repositories":["busybox","centos-sshd"]}
3) /v2/<name>/tags/list
获得镜像标签
# curl -X GET -H "Authorization: Basic YWRtaW46TWlkZWFAMTIz" -H "Accept: */*" http://10.17.153.196:5000/v2/busybox/tags/list
{"name":"busybox","tags":["latest"]}
# curl -X GET -H "Authorization: Basic YWRtaW46TWlkZWFAMTIz" -H "Accept: */*" http://10.17.153.196:5000/v2/centos-sshd/tags/list
{"name":"centos-sshd","tags":["latest"]}
4) 获得一个镜像的Manifest
或者:
5) 删除镜像
通过上面的方式,我们可以获得镜像的digest。例如上面的busybox镜像的digest为:
Docker-Content-Digest: sha256:200c94f114bee03ecf6cc7e671473a30d99d212599c3ad53b822d6a55eec5be5
因此,我们可以用如下的命令来删除:
这里是因为我们禁止了docker registry的删除功能,因此提示Unsupported.
说明:在删除registry2.3或以上版本的docker镜像时,我们通过使用GET或HEAD获取镜像的digest时,必须要加如下参数:
Accept: application/vnd.docker.distribution.manifest.v2+json
3. 附录
下面给出Basic认证条件下,求授权信息信息的方法实现:
运行输出YWRtaW46SGFyYm9yMTIzNDU=
。
[参看]
-
Docker Registry HTTP API V2
-
centos7 Docker私有仓库搭建及删除镜像
-
docker registry 镜像删除