Recently I had a requirement to stats the size of some Docker images. It would be waste if pulling them all firstly then calculating the size of each image. Also you know the docker image consists of some Docker layers that probably are shared by other images. It’s hard to get the disk usage if only sum the size of each image.
Is there any way to get the size of Docker image without pulling it?
It’s definitely Yes. The docker images are hosted by Docker Registry, which is defined by a public specification. The latest V2 of Registry has API to fetch the manifest of an image that contains the size of every layer. Looks like it’s very cool. Utilitying the manifest API of image will satisfie my requirement!
One more thing you should note, the v2 of Docker registry still is compatible with schema specification V1. You have to properly handle with the mixed responses of manifest when you query the manifest of an image.
I created a simple shell script gracefully handling either v1 or v2 response of the image manifest, which can calculate the total layers size of a Docker image with specific tag, or the size of all tags of a Docker image.
Above script was inspired by this post. Hope you enjoy it.