Get the size of a Docker image before a pull?

前端 未结 4 1110
时光说笑
时光说笑 2020-12-30 18:28

How does one get the size of a Docker image before they pull it to their machine?

相关标签:
4条回答
  • 2020-12-30 19:08

    If you really look into the docker code for pull operation, I think your answer is there. If the image of the container is not cached, then during pulling of the image, docker first collects the information about the image from the registry like number of layers, size of each layers etc. etc.

    I would refer to read this file.

    https://github.com/moxiegirl/docker/blob/master/distribution/xfer/download.go

    0 讨论(0)
  • 2020-12-30 19:23

    Docker Hub

    Get the compressed size in bytes of an image specific tag.

    # 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
    

    Get the compressed size in bytes of an image tag for a specific architecture / os.

    # 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
    

    Alternative

    An alternative is to use the experimental docker manifest inspect command. The advantage is that it does not rely on Docker Hub, it works as well with other registries because it's based on the 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
    
    0 讨论(0)
  • 2020-12-30 19:24

    When you search for a docker image on Docker hub, there will be 2 tabs- Repo Info and Tags. Open Tags tab and you will see the sizes of all the types of images you can pull for that image.

    0 讨论(0)
  • 2020-12-30 19:25
    1. For image 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
    
    1. For images on other registry like Microsoft Container Registry

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

      • Use docker save to save 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. To manually view the manifest data

    Use docker manifest inspect to observe the manifest data, which shows you the compressed size of the image.

    • You need to first enable it by editing ~/.docker/config.json file and set experimental to enable. Example: { "experimental": "enabled" }. More info at official docs.

    • Issue docker manifest inspect -v <registry-domain>/<image-name> and see add the size for the layers but only for your specific architecture (e.g. amd64).

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

    Noted:

    1. It's the compressed size of the layers, not their on-disk size on your server.
    2. If the image is a multi-arch image (e.g. alpine linux contains arm, amd64 and several architectures), then you'll get the total of those while in actual usage docker only uses the relevant arch.
    0 讨论(0)
提交回复
热议问题