在拉入之前获取Docker映像的大小?

brtdzjyr  于 2022-12-22  发布在  Docker
关注(0)|答案(6)|浏览(163)

如何获得Docker映像的大小他们将其拉到他们的机器之前?

pbpqsu0x

pbpqsu0x1#

当你在Docker hub上搜索一个Docker图像时,会有2个标签-Repo InfoTags。打开标签标签标签,你会看到所有类型的图像的大小,你可以为该图像拉。

7cwmlq89

7cwmlq892#

1.对于Docker Hub上的图像:

curl -s -H "Authorization: JWT " "https://hub.docker.com/v2/repositories/library/<image-name>/tags/?page_size=100" | jq -r '.results[] | select(.name == "<tag-name>") | .images[0].size' | numfmt --to=iec-i
  • 对于其他注册表(如Microsoft Container Registry)上的映像
  • 将图像推送到Docker Hub,您可以在Docker Hub网站上获得图像的压缩大小。
  • 使用docker save将图像保存为.tar文件,然后将其压缩为. tar.gz文件。
docker save my-image:latest > my-image.tar

# Compress the .tar file
gzip my-image.tar

# Check the size of the compressed image
ls -lh my-image.tar.gz

1.手动查看清单数据
使用docker manifest inspect观察清单数据,它显示了映像的压缩大小。

  • 您需要首先通过编辑~/.docker/config.json文件并将experimental设置为enable来启用它。{ "experimental": "enabled" }。更多信息请访问official docs
  • 发出docker manifest inspect -v <registry-domain>/<image-name>并参见为层添加size,但仅限于您的特定架构(例如amd64)。
docker manifest inspect -v <registry-domain>/<image-name> | grep size | awk -F ':' '{sum+=$NF} END {print sum}' | numfmt --to=iec-i

备注:
1.它是图层的压缩大小,而不是它们在服务器上的磁盘大小。
1.如果映像是多架构映像(例如,alpine linux包含armamd64和多个体系结构),则您将获得这些映像的总和,而在实际使用中,docker仅使用相关的arch

uubf1zoe

uubf1zoe3#

坞站集线器

获取图像特定标记的压缩大小(以字节为单位)。

# for an official image the namespace is called library
curl -s https://hub.docker.com/v2/repositories/library/alpine/tags | \
    jq '.results[] | select(.name=="latest") | .full_size'

# 2796860

# here the project namespace is used
curl -s https://hub.docker.com/v2/repositories/jupyter/base-notebook/tags | \
    jq '.results[] | select(.name=="latest") | .full_size'

# 187647701

获取特定体系结构/操作系统的图像标记的压缩大小(以字节为单位)。

# selecting an architecture
curl -s https://hub.docker.com/v2/repositories/library/alpine/tags | \
    jq '.results[] | select(.name=="latest") | .images[] | select (.architecture=="amd64") | .size'

# 2796860

# selecting an architecture and a specific os
curl -s https://hub.docker.com/v2/repositories/library/hello-world/tags | \                                                                                         
    jq '.results[] | select(.name=="latest") | .images[] | select (.architecture=="amd64" and .os=="linux") | .size'

# 2529

备选

另一种方法是使用实验性的docker manifest inspect命令,优点是它不依赖于Docker Hub,它与其他注册表一起工作,因为它基于Image Manifest specification

# activate experimental mode
export DOCKER_CLI_EXPERIMENTAL=enabled 

docker manifest inspect -v alpine:latest | \
    jq '.[] | select(.Descriptor.platform.architecture=="amd64") | .SchemaV2Manifest.layers[].size'

# 2796860

# need to sum if multiple layers
docker manifest inspect jupyter/base-notebook:latest | \
    jq '[.layers[].size] | add'

# 187647701
o2rvlv0m

o2rvlv0m4#

获取为Image Manifest V2提供服务的任何注册表拉入之前的压缩映像大小:

  • 使用docker manifest inspect(在最新Docker版本中默认可用)
  • 使用jq解析清单中的图层大小并求和
  • 使用numfmt将大小格式化为iec标准(不是si,清单中的大小基于1024)
  • 支持多拱形清单
$ dockersize() { docker manifest inspect -v "$1" | jq -c 'if type == "array" then .[] else . end' |  jq -r '[ ( .Descriptor.platform | [ .os, .architecture, .variant, ."os.version" ] | del(..|nulls) | join("/") ), ( [ .SchemaV2Manifest.layers[].size ] | add ) ] | join(" ")' | numfmt --to iec --format '%.2f' --field 2 | sort | column -t ; }

$ dockersize mcr.microsoft.com/dotnet/core/samples:dotnetapp-buster-slim
linux/amd64  72.96M

$ dockersize ghcr.io/ddelange/pycuda/runtime:3.9-master
linux/amd64  1.84G
linux/arm64  1.80G

$ dockersize python
linux/amd64                    334.98M
linux/arm/v5                   308.21M
linux/arm/v7                   295.69M
linux/arm64/v8                 326.32M
linux/386                      339.74M
linux/mips64le                 314.88M
linux/ppc64le                  343.86M
linux/s390x                    309.52M
windows/amd64/10.0.20348.825   2.20G
windows/amd64/10.0.17763.3165  2.54G
  • 对于Debian用户:apt-get install jq
  • 对于Mac用户:brew install coreutils jq(核心交付numfmt
ix0qys7i

ix0qys7i5#

如果你真的研究了docker的pull操作代码,我想你的答案就在那里。如果容器的图像没有缓存,那么在图像的pull过程中,docker首先从注册表中收集图像的信息,比如层数,每层的大小等等。
我会参考阅读此文件。
https://github.com/moxiegirl/docker/blob/master/distribution/xfer/download.go

gj3fmq9x

gj3fmq9x6#

你可以简单地通过Docker Hub HTTP API获取关于图像的所有信息,如下所示:

https://hub.docker.com/v2/repositories/library/<image_name>/tags/

例如这个URLhttps://hub.docker.com/v2/repositories/library/couchdb/tags/latest返回关于最新的couchdb图像的所有信息

{
  "creator": 2215,
  "id": 2110662,
  "images": [
    {
      "architecture": "amd64",
      "features": "",
      "variant": null,
      "digest": "sha256:12c59b7f8b202476487c670ba7a042b3a654cd91302335df1bfdff0197f92968",
      "os": "linux",
      "os_features": "",
      "os_version": null,
      "size": 87486230,
      "status": "active",
      "last_pulled": "2022-07-25T17:18:08.778757Z",
      "last_pushed": "2022-07-12T15:29:34.553194Z"
    },
    {
      "architecture": "arm64",
      "features": "",
      "variant": "v8",
      "digest": "sha256:5985d1edef0613a93f2d7349a7cac2296ec956674df1f96d70d4eb23e83e6f80",
      "os": "linux",
      "os_features": "",
      "os_version": null,
      "size": 85620381,
      "status": "active",
      "last_pulled": "2022-07-25T15:30:42.25162Z",
      "last_pushed": "2022-07-12T03:05:55.128925Z"
    },
    {
      "architecture": "ppc64le",
      "features": "",
      "variant": null,
      "digest": "sha256:f667d0d4bf4acaeade0cf8510a4a1384ccd66c08e4ab0b678afb7f8651b9df41",
      "os": "linux",
      "os_features": "",
      "os_version": null,
      "size": 93209105,
      "status": "active",
      "last_pulled": "2022-07-25T12:28:16.460623Z",
      "last_pushed": "2022-07-12T05:26:46.295412Z"
    }
  ],
  "last_updated": "2022-07-12T15:30:03.436304Z",
  "last_updater": 1156886,
  "last_updater_username": "doijanky",
  "name": "latest",
  "repository": 545837,
  "full_size": 87486230,
  "v2": true,
  "tag_status": "active",
  "tag_last_pulled": "2022-07-25T17:18:08.778757Z",
  "tag_last_pushed": "2022-07-12T15:30:03.436304Z"
}

图像大小(字节)为:87486230
如果你只需要完整的大小,你可以通过任何json处理器,比如jq或其他,获取json并提取属性full_size
例如:

curl -s https://hub.docker.com/v2/repositories/library/couchdb/tags/latest | jq 'select(.name=="latest") | .full_size'
//87486230

相关问题