如何通过 **rest** api 给定配置推送 docker 图像
how to push docker image via **rest** api given config
我想通过仅提供部分数据在远程 docker 注册表中创建一个新图像:
根据文档
https://docs.docker.com/registry/spec/api/#pushing-an-image
为了推送 docker 图片,我可以:
* post 我有一个 tar 层。
* post 清单
注册表将支持我的新图像。
例如:
* 我在本地的 tar 层中有一个 java 应用程序。
* 远程 docker 注册表已经有一个 java8 基本映像。
* 我想上传 tar 层和引用 java8 基本图像的清单,并让 docker 注册表支持我的应用程序的新图像。
(层 tar 我从名为 Bazel 的第 3 方构建工具获得,如果有人关心的话)
根据我收集到的文档,我可以获取现有的 java8 图像清单,下载它,将我的新图层附加(或预先附加)到图层部分和中提琴。
查看清单规范
https://docs.docker.com/registry/spec/manifest-v2-2/#image-manifest-field-descriptions
我看到有一个 "config object" 部分,其中摘要作为配置文件的参考。这是有道理的,例如我可能需要重新定义入口点。所以假设我在文件中也有一个 docker 配置,我想我需要让注册表以某种方式知道。
API 中没有任何地方(我可以看到)是否说明上传配置的位置或方式,或者我是否需要这样做 - 也许它包含在 tar 层中什么的。
我是否将配置作为图层上传?它包含在 tar 中吗?如果不是,为什么我要通过摘要引用它?
我希望得到的最佳答案是对 docker-注册表的一系列 http 调用,它们可以完成我正在尝试的操作。或者,仅解释配置是什么以及如何进行配置将非常有帮助。
在此处找到解决方案:
https://www.danlorenc.com/posts/containers-part-2/
非常详细,很好的答案,不知道你是谁,但我爱你!
从现有图像中检查一些配置,Docker 似乎需要几个字段:
{
"architecture": "amd64",
"config": {
},
"history": [
{
"created_by": "Bash!"
}
],
"os": "linux",
"rootfs": {
"type": "layers",
"diff_ids": [
"sha256:69e4bd05139a843cbde4d64f8339b782f4da005e1cae56159adfc92311504719"
]
}
}
配置部分可以包含环境变量、容器的默认 CMD 和 ENTRYPOINT 以及一些其他设置。 rootfs 部分包含层列表和 diff_ids,看起来与我们的清单非常相似。不幸的是,diff_ids 实际上与我们的清单中包含的摘要略有不同,它们实际上是“未压缩”层的 sha256。
我们可以用这个脚本创建一个:
cat <<EOF > config.json
{
"architecture": "amd64",
"config": {
},
"history": [
{
"created_by": "Bash!"
}
],
"os": "linux",
"rootfs": {
"type": "layers",
"diff_ids": [
"sha256:$(gunzip layer.tar.gz --to-stdout | shasum -a 256 | cut -d' ' -f1)"
]
}
}
EOF
配置上传
配置基本上由注册表存储为普通 blob。它们在清单中的引用方式不同,但它们仍按摘要上传并正常存储。
我们用于层的相同类型的脚本将在这里工作:
returncode=$(curl -w "%{http_code}" -o /dev/null \
-I -H "Authorization: Bearer $(gcloud auth print-access-token)" \
https://gcr.io/v2/$PROJECT/hello/blobs/$config_digest)
if [[ $returncode -ne 200 ]]; then
# Start the upload and get the location header.
# The HTTP response seems to include carriage returns, which we need to strip
location=$(curl -i -X POST \
https://gcr.io/v2/$PROJECT/hello/blobs/uploads/ \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-d "" | grep Location | cut -d" " -f2 | tr -d '\r')
# Do the upload
curl -X PUT $location\?digest=$config_digest \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
--data-binary @config.json
fi
我想通过仅提供部分数据在远程 docker 注册表中创建一个新图像:
根据文档
https://docs.docker.com/registry/spec/api/#pushing-an-image
为了推送 docker 图片,我可以:
* post 我有一个 tar 层。
* post 清单
注册表将支持我的新图像。
例如:
* 我在本地的 tar 层中有一个 java 应用程序。
* 远程 docker 注册表已经有一个 java8 基本映像。
* 我想上传 tar 层和引用 java8 基本图像的清单,并让 docker 注册表支持我的应用程序的新图像。
(层 tar 我从名为 Bazel 的第 3 方构建工具获得,如果有人关心的话)
根据我收集到的文档,我可以获取现有的 java8 图像清单,下载它,将我的新图层附加(或预先附加)到图层部分和中提琴。
查看清单规范 https://docs.docker.com/registry/spec/manifest-v2-2/#image-manifest-field-descriptions 我看到有一个 "config object" 部分,其中摘要作为配置文件的参考。这是有道理的,例如我可能需要重新定义入口点。所以假设我在文件中也有一个 docker 配置,我想我需要让注册表以某种方式知道。
API 中没有任何地方(我可以看到)是否说明上传配置的位置或方式,或者我是否需要这样做 - 也许它包含在 tar 层中什么的。
我是否将配置作为图层上传?它包含在 tar 中吗?如果不是,为什么我要通过摘要引用它?
我希望得到的最佳答案是对 docker-注册表的一系列 http 调用,它们可以完成我正在尝试的操作。或者,仅解释配置是什么以及如何进行配置将非常有帮助。
在此处找到解决方案:
https://www.danlorenc.com/posts/containers-part-2/
非常详细,很好的答案,不知道你是谁,但我爱你!
从现有图像中检查一些配置,Docker 似乎需要几个字段:
{
"architecture": "amd64",
"config": {
},
"history": [
{
"created_by": "Bash!"
}
],
"os": "linux",
"rootfs": {
"type": "layers",
"diff_ids": [
"sha256:69e4bd05139a843cbde4d64f8339b782f4da005e1cae56159adfc92311504719"
]
}
}
配置部分可以包含环境变量、容器的默认 CMD 和 ENTRYPOINT 以及一些其他设置。 rootfs 部分包含层列表和 diff_ids,看起来与我们的清单非常相似。不幸的是,diff_ids 实际上与我们的清单中包含的摘要略有不同,它们实际上是“未压缩”层的 sha256。
我们可以用这个脚本创建一个:
cat <<EOF > config.json
{
"architecture": "amd64",
"config": {
},
"history": [
{
"created_by": "Bash!"
}
],
"os": "linux",
"rootfs": {
"type": "layers",
"diff_ids": [
"sha256:$(gunzip layer.tar.gz --to-stdout | shasum -a 256 | cut -d' ' -f1)"
]
}
}
EOF
配置上传 配置基本上由注册表存储为普通 blob。它们在清单中的引用方式不同,但它们仍按摘要上传并正常存储。
我们用于层的相同类型的脚本将在这里工作:
returncode=$(curl -w "%{http_code}" -o /dev/null \
-I -H "Authorization: Bearer $(gcloud auth print-access-token)" \
https://gcr.io/v2/$PROJECT/hello/blobs/$config_digest)
if [[ $returncode -ne 200 ]]; then
# Start the upload and get the location header.
# The HTTP response seems to include carriage returns, which we need to strip
location=$(curl -i -X POST \
https://gcr.io/v2/$PROJECT/hello/blobs/uploads/ \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-d "" | grep Location | cut -d" " -f2 | tr -d '\r')
# Do the upload
curl -X PUT $location\?digest=$config_digest \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
--data-binary @config.json
fi