Sub topic 6
Measuring size with docker image ls and docker history
This chapter assumes you can build an image from a Dockerfile, run a container with docker run, and know that an image is a stack of layers. You will learn to read the size numbers Docker reports, find which layer is responsible for them, and avoid common misreadings. Without measurements, size work is guesswork. With them, you can show that a change helped.
What docker image ls reports
docker image ls lists local images. The SIZE column is the total uncompressed size of all the image's layers on disk, including the base image's layers.
docker image ls myappExample output (the numbers are illustrative):
REPOSITORY TAG IMAGE ID CREATED SIZE
myapp small 3f1c9a7be210 2 minutes ago 12.4MB
myapp build a98d0c44f5e1 3 minutes ago 891MBSome details affect how you read this table:
- Docker prints decimal units, so 1 MB is 1,000,000 bytes, not 1,048,576. Compare numbers produced by the same tool.
- The size is uncompressed. A registry stores and transfers compressed layers, so the download is usually smaller than the
SIZEshown here. - Layers shared between images are counted in full for each image. Two images built from the same base each show the base's size, even though it exists once on disk.
- Newer Docker versions that use the containerd image store may show extra columns, such as content size and disk usage, in place of a single size. Read the header of your own output before comparing numbers.
Getting just the number
For scripts and comparisons, a format string removes the noise.
docker image ls --format "table {{.Repository}}:{{.Tag}}\t{{.Size}}" myapp
docker image inspect --format '{{.Size}}' myapp:smallThe first command prints a compact table. The second prints the size in bytes, which is easy to compare in a CI job.
Actual disk use
Because of shared layers, adding the SIZE values of all your images overstates what Docker uses. For real disk consumption, use:
docker system df
docker system df -vThe -v form lists each image with a SHARED SIZE and a UNIQUE SIZE. Unique size is what you would free by deleting that image alone.
Reading layers with docker history
docker image ls tells you the total. docker history tells you where it came from. It prints one row per layer, newest first, with the instruction that created it.
docker history myapp:smallThe columns are:
| Column | Meaning |
|---|---|
| `IMAGE` | The layer's ID, or `<missing>` if the layer was not built locally |
| `CREATED` | When the layer was created |
| `CREATED BY` | The instruction that produced the layer, truncated by default |
| `SIZE` | The bytes this layer added to the filesystem |
| `COMMENT` | Extra information, often empty |
A 0B size is normal. Instructions that only change metadata, such as ENV, CMD, ENTRYPOINT, EXPOSE and LABEL, add no filesystem content. RUN, COPY and ADD are where the bytes come from.
Two options make the output easier to use:
docker history --no-trunc myapp:small
docker history --format "{{.Size}}\t{{.CreatedBy}}" myapp:small--no-trunc shows the full command for each layer. --format lets you print only the columns you care about.
Why you see missing layers
If you pull an image such as python:3.12-slim and run docker history on it, most rows show <missing> as the ID. This is expected. Those layers came from a registry, not from a local build, so Docker has no local image ID for them. The sizes and commands are still correct.
Layers only add: a worked example
A file deleted in a later layer does not shrink the image. The earlier layer still contains the file, and the later layer only records that it was removed. You can see this by building a deliberately wasteful image.
FROM alpine:3.20
RUN dd if=/dev/zero of=/big.bin bs=1M count=100
RUN rm /big.binSave this as Dockerfile.wasteful and run the commands below.
docker build -f Dockerfile.wasteful -t wasteful .
docker image ls wasteful
docker history wastefulThe history will look roughly like this:
IMAGE CREATED CREATED BY SIZE
<id> 10 seconds ago RUN /bin/sh -c rm /big.bin # buildkit 0B
<id> 11 seconds ago RUN /bin/sh -c dd if=/dev/zero of=/big.bin ... 105MB
<missing> 2 months ago CMD ["/bin/sh"] 0B
<missing> 2 months ago ADD alpine-minirootfs-3.20...tar.gz / # ... 7.8MBThe container has no /big.bin, yet the image is about 113 MB. The rm layer reports 0B because it adds only a deletion marker. It does not give the 105 MB back.
The fix is to do the creation and deletion in a single RUN, so the file never lands in a committed layer, or to use a multi-stage build so the temporary files stay in a stage you discard. The next chapters cover the second approach.
Comparing stages with --target
Multi-stage builds produce an image that hides the size of the build tooling. To see how much you saved, build the final stage and an intermediate stage, then compare them. This is the sequence from the chapter plan:
docker build -t myapp:small .
docker build --target build -t myapp:build .
docker image ls myapp
docker history myapp:smallThe first command builds the whole Dockerfile and tags the last stage as myapp:small. The second stops at the stage named build and tags it as myapp:build. The third lists both so you can compare sizes. The fourth shows what is inside the small image.
--target requires the stage to have a name with FROM ... AS build. Naming stages is covered in a later chapter. For now, treat myapp:build as a measurement of "what the image would weigh without multi-stage".
Finding the largest layers
When an image is bigger than you expect, work through these steps:
- Run
docker image lsto get the total. - Run
docker history --no-truncand look for the largestSIZEvalues. - Match each large row to a line in your Dockerfile.
- Ask what that layer contains and whether the final image needs it.
Typical causes you will find are package manager caches left behind by apt-get install, a COPY . . that pulled in node_modules or .git, and a build toolchain that is still present at runtime.
Common mistakes when measuring
- Comparing different units. The registry page for an image may show compressed size, while
docker image lsshows uncompressed size. A 30 MB download can be a 90 MB image. - Summing image sizes. Shared base layers make the total of
SIZEvalues larger than real disk use. Usedocker system df. - Trusting
0Brows. A0Blayer that follows a large layer may be hiding wasted space, as in thermexample. - Measuring after a cache hit by assumption. Size does not depend on cache hits, but the
CREATEDcolumn does. An old timestamp on a layer you just rebuilt means Docker reused the cached layer. - Reading
<missing>as an error. It only means the layer was pulled rather than built here.
Exercises
Exercise
You run docker history on an image. One layer created by RUN npm install shows 280MB, and the next layer, RUN rm -rf node_modules, shows 0B. Does the image contain 280 MB of node_modules? Explain.
Show answer
Yes, the 280 MB is still part of the image. The rm layer only records that the directory was deleted, so it adds almost nothing. The earlier layer still holds the files, and they count toward the image size. To avoid this, install and remove in the same RUN, or build in an earlier stage and copy only what is needed.
Exercise
Two images, a and b, are built from the same 80 MB base image. Each shows SIZE of 100MB in docker image ls. How much disk do they use together, roughly, and which command shows it?
Show answer
About 120 MB, not 200 MB. The 80 MB base is stored once and each image adds about 20 MB of its own layers. docker system df -v shows the shared and unique sizes for each image.