Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker image size

Tags:

docker

I pull a Docker image of about 0.5 GB in size on the Docker Hub. After pulling it on my Centos machine the size of the image became 1.6 GB. Pushing the image with a new name show 2 GB on the Docker hub. How can I obtain an image with the same size on the Docker hub?

like image 866
Dragomir Adrian Avatar asked Dec 06 '15 04:12

Dragomir Adrian


People also ask

Is there a limit to Docker image size?

The maximum size for a deployable container image on Azure Container Instances is 15 GB.

Why is Docker image size so large?

Docker doesn't know what changes have happened inside a layer, only which files are affected. As such, this will cause Docker to create a new layer, replacing all those files (same content as /opt/jboss/wildfly/ but with with new ownership), adding hundreds of megabytes to image size.

Why is Docker image size so small?

The reason behind this is that docker would be able to cache the image with the required dependencies, and this cache can then be used in the following builds when code gets modified.

Does size of Docker image matter?

Docker images are a core component in our development and production lifecycles. Having a large image can make every step of the process slow and tedious. Size affects how long it takes to build the image locally, on CI/CD, or in production and it affects how quickly we can spin up new instances that run our code.


2 Answers

There are several ways to achieve that, depending on which repository you are querying.

TLDR; See this repo: schnatterer/docker-image-size.

Details:

The former two work for DockerHub and returned same size for the test image debian:stretch-20190204-slim on DockerHub:

Screenshot DockerHub

I didn't find a working solution for quay.io and for private Repos in general, yet.

docker manifest

Requires jq.

export DOCKER_CLI_EXPERIMENTAL=enabled

echo $(( ( $(docker manifest inspect -v docker.io/debian:stretch-20190204-slim \
              | jq '.[] | select(.Descriptor.platform.architecture == "amd64").SchemaV2Manifest.layers[0].size') \
           + 500000) \
         / 1000 \
         / 1000)) MB
23 MB

If you care about the details:

  • export DOCKER_CLI_EXPERIMENTAL=enabled - docker manifest is still considered experimental (as of 18.06.1-ce). Can be made permanent in ~/.docker/config.json - See docker manifest | Docker Documentation
  • docker manifest inspect -v docker.io/debian:stretch-20190204-slim - queries verbose manifest from docker hub
  • jq '.[] | select(.Descriptor.platform.architecture == "amd64").SchemaV2Manifest.layers[0].size - filters the compressed size of all layers (bytes)
  • + 500000 - for rounding
  • /1000 - divide to KB/MB

reg

Requires reg and jq.

➜ echo $(( ( $(reg manifest debian:stretch-20190204-slim |  \
            jq '.layers[].size' \
            | paste -sd+ | bc) \
           + 500000) \
         / 1000 \
         / 1000)) MB
23 MB

If you care about the details:

  • reg manifest debian:stretch-20190204-slim - queries manifest from docker hub
  • jq '.layers[].size' - filters the compressed size of each layer (bytes)
  • paste -sd+ | bc - creates a sum of the individual sizes (bytes)
  • + 500000 - for rounding
  • /1000 - divide to KB/MB

curl

reg and docker manifest didn't work out of the box for some other repos. Here, curl might be sufficient, however. This, OTOH, is a bit more difficult with docker hub (because we need a token).

➜ echo $(( ( $(curl -s https://gcr.io/v2/distroless/java/manifests/11-debug |  \
            jq '.layers[].size' \
            | paste -sd+ | bc) \
           + 500000) \
         / 1000 \
         / 1000)) MB
69 MB

Further Registry examples:

  • https://quay.io/v2/calico/node/manifests/v3.2.4-2-g41efb10-amd64
  • https://mcr.microsoft.com/v2/dotnet/core/samples/manifests/aspnetapp
like image 200
schnatterer Avatar answered Nov 01 '22 08:11

schnatterer


A similar issue was reported in issue 14204, for docker 1.7.0 on Ubuntu.
(And by default, CentOS might not have the latest version of docker, so the first step would be to upgrade if possible)

The questions to check were:

  1. How did you install docker?
  2. Can you provide the list the steps to reproduce the issue?
  3. Can you post the output from sudo du -sh /var/lib/docker/*
  4. You are using devicemapper as a storage driver, can you try using aufs?

For the last point, as described in this article, check /etc/default/docker

# Use DOCKER_OPTS to modify the daemon startup options.
DOCKER_OPTS="--storage-driver=devicemapper"

As the OP Dragomir Adrian confirms in the comments, it is a docker version issue: upgrading to 1.9.1 helps.

like image 37
VonC Avatar answered Nov 01 '22 09:11

VonC