侧边栏壁纸
  • 累计撰写 214 篇文章
  • 累计创建 59 个标签
  • 累计收到 5 条评论

Docker Registry API 常用接口

barwe
2024-09-25 / 0 评论 / 0 点赞 / 46 阅读 / 2,404 字
温馨提示:
本文最后更新于 2024-09-25,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

假设提供 Registry 服务的地址是 https://myregistry.com

获取仓库列表

curl -H "Accept: application/vnd.docker.distribution.manifest.v2+json" \
-H "Authorization: Basic <TOKEN>" \
"https://myregistry.com/v2/_catalog"

其中,<TOKEN> 替换成登录凭据(如果有的话)。

响应数据格式为:

{
    "repositories": ["repo1", "repo2"]
}

获取指定仓库的标签列表

curl -H "Accept: application/vnd.docker.distribution.manifest.v2+json" \
-H "Authorization: Basic <TOKEN>" \
"https://myregistry.com/v2/<REPOSITORY>/tags/list"

其中,<TOKEN> 替换成登录凭据(如果有的话),<REPOSITORY> 替换为仓库名。

响应数据格式为:

{
    "name": "<REPOSITORY>",
    "tags": ["tag1", "tag2"]
}

获取指定镜像的元数据

这里的“镜像”指的是一个 repository:tag

curl -H "Accept: application/vnd.docker.distribution.manifest.v2+json" \
-H "Authorization: Basic <TOKEN>" \
"https://myregistry.com/v2/<REPOSITORY>/manifests/<TAG>"

其中,<TOKEN> 替换成登录凭据(如果有的话),<REPOSITORY> 替换为仓库名,<TAG> 替换成镜像标签。

响应数据格式为:

{
   "schemaVersion": 2,
   "mediaType": "application/vnd.docker.distribution.manifest.v2+json",
   "config": {
      "mediaType": "application/vnd.docker.container.image.v1+json",
      "size": 7015,
      "digest": "sha256:e042c0d76c68e867a8e0e9234257ec70d2bb58628b1f0bd33363f346f9ca0e11"
   },
   "layers": [
      {
         "mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
         "size": 28584024,
         "digest": "sha256:521f275cc58bdab90307a5929f8a6d197368b8c214edbc7f06fc1aaf48cfff3e"
      }
   ]

删除指定镜像

官方提供的删除 repository:tag 的接口:

curl -X DELETE \
-H "Accept: application/vnd.docker.distribution.manifest.v2+json" \
-H "Authorization: Basic <TOKEN>" \
"https://myregistry.com/v2/<REPOSITORY>/manifests/<CONTENT_DIGEST>"

其中,<TOKEN> 替换成登录凭据(如果有的话),<REPOSITORY> 替换为仓库名,<CONTENT_DIGEST> 替换成镜像的 Content Digest。

Registry API 没有提供直接获取 Content Digest 的接口,需要自己计算镜像元数据的 SHA256 值。

以 Python 为例:

import hashlib
import requests

headers = {
    "Authorization": "Basic <TOKEN>",
    "Accept": "application/vnd.docker.distribution.manifest.v2+json",
}
response = requests.get("https://myregistry.com/v2/<REPOSITORY>/manifests/<TAG>", headers=headers)
content_digest = "sha256:" + hashlib.sha256(response.text.encode("utf-8")).hexdigest()
0

评论区