如何使用Docker Registry API提取有关容器的信息?获得UNAUTHORIZED

问题描述 投票:3回答:1

我偶然发现了一篇名为Inspecting Docker Images without pulling them的文章,该文章深入探讨了基本上使用REST调用进行docker inspect所需的特定API调用的细节。但是,我想知道自从撰写该文章以来,Docker注册表API是否发生了一些变化。

文章分析说,您需要按顺序进行三次REST调用,以获取有关容器的信息。在公共Docker注册表的情况下,它们如下:

  1. 向auth.docker.io发出GET请求以获取令牌 curl "https://auth.docker.io/token?scope=repository:<image>:pull&service=registry.docker.io" 在这种情况下,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>" 这里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>" 这会返回一些JSON,我关心的字段是.config

我能够在私有Docker注册表上成功测试,尽管我必须为auth做一些不同的事情。但是当我尝试跟随公共Docker注册表中的指南(我在上面的这些步骤中概述)时,我遇到了相反的问题:第1步给了我一个令牌,但是这个令牌毫无价值。每当我尝试使用它时,在步骤2或3中,我得到了回复:

{"errors":[{"code":"UNAUTHORIZED","message":"authentication required","detail":[{"Type":"repository","Class":"","Name":"docker","Action":"pull"}]}]}

关于如何使这个工作的任何想法?

docker curl docker-registry
1个回答
3
投票

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

  1. 获取图像的相应标记。请注意,您必须指定图像的全名(官方图像使用library存储库)。因此,NGINX image应该被称为:library/nginxcurl \ --silent \ "https://auth.docker.io/token?scope=repository:library/nginx:pull&service=registry.docker.io" \ | jq -r '.token' 令牌简洁缩短: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' SHA256:2bcb04bdb83f7c5dc30f0edaca1609a716bda1c7d2244d4f5fbbdfef33da366c
  3. 最后,使用以下请求获取容器配置。在URL中,必须指定步骤2中的摘要。 curl \ --silent \ --location \ --header "Authorization: Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXV..." \ "https://registry-1.docker.io/v2/library/nginx/blobs/sha256:2bcb04bdb83f7c5dc30f0edaca1609a716bda1c7d2244d4f5fbbdfef33da366c" \ | jq -r '.container_config' 缩短输出以简化: { "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 <[email protected]>" }, "StopSignal": "SIGTERM" }
© www.soinside.com 2019 - 2024. All rights reserved.