我偶然发现了一篇名为Inspecting Docker Images without pulling them的文章,该文章深入探讨了基本上使用REST调用进行docker inspect
所需的特定API调用的细节。但是,我想知道自从撰写该文章以来,Docker注册表API是否发生了一些变化。
文章分析说,您需要按顺序进行三次REST调用,以获取有关容器的信息。在公共Docker注册表的情况下,它们如下:
curl "https://auth.docker.io/token?scope=repository:<image>:pull&service=registry.docker.io"
在这种情况下,image
可能像nginx
或docker
- 基本上你正在查找的任何图像。此REST调用返回一个令牌,以便在后续请求中使用。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
中提取值。这是我们在最终请求中使用的摘要字符串。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"}]}]}
关于如何使这个工作的任何想法?
使用以下步骤,您可以检索任何公共容器图像的配置。
library
存储库)。因此,NGINX image应该被称为:library/nginx
。
curl \
--silent \
"https://auth.docker.io/token?scope=repository:library/nginx:pull&service=registry.docker.io" \
| jq -r '.token'
令牌简洁缩短:eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXV ......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:2bcb04bdb83f7c5dc30f0edaca1609a716bda1c7d2244d4f5fbbdfef33da366ccurl \
--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"
}