不久前我看到一篇文章,标题为 Inspecting Docker Images without pulling them它深入了解了使用 REST 调用进行 docker 检查
所需的特定 API 调用的本质。不过,我想知道自那篇文章撰写以来,Docker 注册表 API 是否发生了一些变化。
本文指出,您需要按顺序进行三个 REST 调用才能获取有关容器的信息。对于公共(public) Docker 注册表,它们如下:
向 auth.docker.io 发出 GET 请求以获取 token
curl "https://auth.docker.io/token?scope=repository:<image>:pull&service=registry.docker.io"
在这种情况下,
image
可能类似于nginx
或docker
- 基本上是您要查找的任何图像。此 REST 调用返回一个 token 以在后续请求中使用。检索 list 列表的 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
处的值。这是我们在最终请求中使用的摘要字符串。最后一个 GET 请求,使用我们在步骤 2 中收到的摘要来检索容器配置
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
我能够在私有(private) Docker 注册表上成功测试这一点,尽管我必须为身份验证做一些不同的事情。然而,当我尝试遵循公共(public) Docker 注册表的指南(我在上面的这些步骤中概述)时,我遇到了相反的问题:步骤 1 给了我一个 token ,但该 token 毫无值(value)。每当我尝试使用它时,在步骤 2 或 3 中,我都会得到以下信息:
{"errors":[{"code":"UNAUTHORIZED","message":"authentication required","detail":[{"Type":"repository","Class":"","Name":"docker","Action":"pull"}]}]}
关于如何让它发挥作用有什么想法吗?
最佳答案
使用以下步骤,您可以检索任何公共(public)容器镜像的配置。
获取图像的相应标记。请注意,您必须指定镜像的全名(官方镜像使用
library
存储库)。从而,NGINX image应引用为:library/nginx
。curl \ --silent \ "https://auth.docker.io/token?scope=repository:library/nginx:pull&service=registry.docker.io" \ | jq -r '.token'
Token shortened for brevity: eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXV...
从 list 中检索图像摘要。对于此请求,您还需要指定一个有效标签(本示例使用
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
最后,使用以下请求获取容器配置。在 URL 中,必须指定第二步的摘要。
curl \ --silent \ --location \ --header "Authorization: Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXV..." \ "https://registry-1.docker.io/v2/library/nginx/blobs/sha256:2bcb04bdb83f7c5dc30f0edaca1609a716bda1c7d2244d4f5fbbdfef33da366c" \ | jq -r '.container_config'
Output shortened for brevity:
{ "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" }
关于docker - 如何使用 Docker 注册表 API 提取有关容器的信息?未经授权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55386202/