Docker镜像的下载大小

5
当拉取 Docker 镜像时,它会将其分为不同的部分(层)。在实际下载之前,我需要获取镜像所有必需层的下载大小。
有没有办法做到这一点?
可以运行 docker pull 命令并观察输出来获取此信息。
ffcacfbccecb: Downloading [+++++>    ] 14.1 MB/30.13 MB
ffcdbdebabbe: Downloading [++>       ] 1.1 MB/12.02 MB

因此,它的下载大小为“42.15”。

但是,我已经启用了一些选项以逐个下载图层:

ffcacfbccecb: Downloading [+++++>    ] 14.1 MB/30.13 MB
ffcdbdebabbe: Waiting

所以这个解决方案对我无效。

1
在下降投票时,始终留下评论,以便原贴作者知道如何改进问题,尤其是对于那些您没有浪费任何声望的问题,因此您欠原贴作者一个解释。 :-) - Ravexina
4个回答

4

很遗憾,Docker Hub API没有公开的文档。但是您可以获取一个JWT用于API,然后调用列表以获取大小。这里有一个示例,使用jq解析字节大小:

首先进行身份验证以获取令牌:

export HUBUSER=andyshinn
export HUBPASS=mypass
export HUBTOKEN=$(curl -s -H "Content-Type: application/json" -X POST -d '{"username": "'${HUBUSER}'", "password": "'${HUBPASS}'"}' https://hub.docker.com/v2/users/login/ | jq -r .token)

现在您可以查询标签 API,以筛选特定标签并获取其大小。在此示例中,我们正在获取官方库wordpress图像,并过滤掉latest标签:

curl -s -H "Authorization: JWT ${TOKEN}" "https://hub.docker.com/v2/repositories/library/wordpress/tags/?page_size=100" | jq -r '.results[] | select(.name == "latest") | .images[0].size'

你应该会得到一个类似于169817871的东西,它表示整个图像的字节数。这是从https://gist.github.com/kizbitz/e59f95f7557b4bbb8bf2中找到的信息的修改示例。

2
值得一提的是,我们可以将 curl 的输出通过管道传递给 numfmt --to=iec-i 命令,以获得类似于 162Mi 这样的人类可读数字。 - Ravexina

2

@Andy的方法很完美。非常感谢!那确实帮了我很多。

  1. In reference to his answer, I use the below command to get the compressed size of a Docker image hosted on 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
    
  2. For an image on another registry, like Microsoft Container Registry, I figured out three ways:

    • Use docker manifest inspect to observe the manifest data, which can give you idea on how to gain the compressed size of the image.

      docker manifest inspect -v <registry-domain>/<image-name> | grep size | awk -F ':' '{sum+=$NF} END {print sum}' | numfmt --to=iec-i
      

      To enable docker manifest inspect, edit ~/.docker/config.json file and set experimental to enable.(Reference: Docker manifest inspect)

    • Push the image to Docker Hub and you can get the compressed size of the image on the Docker Hub website.

    • Use docker save to save an image to a .tar file and then compress it a .tar.gz file.

      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页面,例如:MySQL容器标签,正如你在这张图片中所看到的粗体文本。 我希望这能帮助你和其他人。


0

然而,@Andy提供的答案更像是程序员的选择和更好的方法。我通常使用另一种我认为更容易和方便的方式。

我经常使用在线实验室来测试Docker镜像的大小和性能。我最喜欢的是play-with-docker,它提供了大约3小时的终端会话,您可以自由地做任何事情。镜像在那里很容易且非常快速地下载。之后,我可以获得有关图像的所有所需信息,无论是大小还是其他任何内容。我还可以通过在那里使用它来查看该图像是否适合我的工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接