如何使用 Docker 注册表 API 提取有关容器的信息?未经授权

soa*_*gem 5 curl docker docker-registry

不久前,我看到一篇题为“Inspecting Docker Images without Pulling They”docker inspect的文章,深入了解了使用REST 调用进行操作所需的特定 API 调用的本质。然而,我想知道自那篇文章撰写以来,Docker 注册表 API 是否发生了一些变化。

本文指出,您需要按顺序进行三个 REST 调用才能获取有关容器的信息。对于公共 Docker 注册表,它们如下:

  1. 向 auth.docker.io 发出 GET 请求以获取令牌

    curl "https://auth.docker.io/token?scope=repository:<image>:pull&service=registry.docker.io"
    
    Run Code Online (Sandbox Code Playgroud)

    在这种情况下,image可能是类似nginxdocker- 基本上是您正在查找的任何图像。此 REST 调用返回一个令牌以在后续请求中使用。

  2. 用于检索清单列表的 GET 请求

    curl -H "Accept: application/vnd.docker.distribution.manifest.v2+json"
    -H "Authorization: Bearer <token-from-step-1>"
    "https://registry-1.docker.io/v2/<image>/manifests/<tag>"
    
    Run Code Online (Sandbox Code Playgroud)

    image与步骤 1 中的相同,tag可能类似于latest. 此调用返回一些 JSON;关键是我们需要提取 处的值.config.digest。这是我们在最终请求中使用的摘要字符串。

  3. 最后,使用我们在步骤 2 中收到的摘要来检索容器配置的 GET 请求

    curl -H "Accept: application/vnd.docker.distribution.manifest.v2+json"
    -H "Authorization: Bearer <token-from-step-1>"
    "https://registry-1.docker.io/v2/<image>/blobs/<digest-from-step-2>"
    
    Run Code Online (Sandbox Code Playgroud)

    这会返回一些 JSON,我关心的字段是.config

我能够在私有 Docker 注册表上成功测试这一点,尽管我必须为身份验证做一些不同的事情。然而,当我尝试遵循公共 Docker 注册表的指南(我在上面的这些步骤中概述)时,我遇到了相反的问题:步骤 1 给了我一个令牌,但该令牌毫无价值。每当我尝试使用它时,在步骤 2 或 3 中,我都会得到以下信息:

{"errors":[{"code":"UNAUTHORIZED","message":"authentication required","detail":[{"Type":"repository","Class":"","Name":"docker","Action":"pull"}]}]}
Run Code Online (Sandbox Code Playgroud)

关于如何让它发挥作用有什么想法吗?

prd*_*prd 8

使用以下步骤,您可以检索任何公共容器映像的配置。

  1. 获取图像的相应标记。请注意,您必须指定镜像的全名(官方镜像使用 library存储库)。因此,NGINX 镜像应称为:library/nginx

    curl \
        --silent \
        "https://auth.docker.io/token?scope=repository:library/nginx:pull&service=registry.docker.io" \
        | jq -r '.token'
    
    Run Code Online (Sandbox Code Playgroud)

    为简洁起见,令牌被缩短: eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXV...

  2. 从清单中检索图像摘要。对于此请求,您还需要指定一个有效的标签(本示例使用该latest标签)。

    curl \
        --silent \
        --header "Accept: application/vnd.docker.distribution.manifest.v2+json" \
        --header "Authorization: Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXV..." \
        "https://registry-1.docker.io/v2/library/nginx/manifests/latest" \
        | jq -r '.config.digest'
    
    Run Code Online (Sandbox Code Playgroud)

    sha256:2bcb04bdb83f7c5dc30f0edaca1609a716bda1c7d2244d4f5fbbdfef33da366c

  3. 最后,使用以下请求获取容器配置。在 URL 中,必须指定第二步中的摘要。

    curl \
        --silent \
        --location \
        --header "Authorization: Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXV..." \
        "https://registry-1.docker.io/v2/library/nginx/blobs/sha256:2bcb04bdb83f7c5dc30f0edaca1609a716bda1c7d2244d4f5fbbdfef33da366c" \
        | jq -r '.container_config'
    
    Run Code Online (Sandbox Code Playgroud)

    为简洁起见,缩短了输出:

    {
        "Hostname": "6c02a05b3d09",
        "Env": [
            "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
            "NGINX_VERSION=1.15.10-1~stretch",
            "NJS_VERSION=1.15.10.0.3.0-1~stretch"
        ],
        "Labels": {
            "maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>"
        },
        "StopSignal": "SIGTERM"
    }
    
    Run Code Online (Sandbox Code Playgroud)