Sub topic 3
Listing layers with docker history
This chapter assumes you can build and run images, read a simple Dockerfile, and use a Linux shell. You should also know that an image is a stack of layers, and that most Dockerfile instructions add one. Here you will use docker history to list those layers and see what each one costs.
What docker history shows
docker history IMAGE prints one row per layer, newest first. Each row shows the layer's ID, its age, the instruction that created it, its size, and any comment. It reads only the image's stored metadata, so it is fast and needs no extra tools.
The Dockerfile below is deliberately wasteful. We will use it as the running example.
FROM debian:bookworm-slim
RUN apt-get update
RUN apt-get install -y curl
COPY data.tar /tmp/data.tar
RUN tar -xf /tmp/data.tar -C /opt
RUN rm /tmp/data.tarBuild it and list its layers:
docker build -t demo:wasteful .
docker history demo:wastefulThe output looks like this. Your IDs, ages, and sizes will differ.
IMAGE CREATED CREATED BY SIZE COMMENT
a1b2c3d4e5f6 1 minute ago RUN /bin/sh -c rm /tmp/data.tar # buildkit 0B buildkit.dockerfile.v0
<missing> 1 minute ago RUN /bin/sh -c tar -xf /tmp/data.tar -C /op… 48MB buildkit.dockerfile.v0
<missing> 1 minute ago COPY data.tar /tmp/data.tar # buildkit 48MB buildkit.dockerfile.v0
<missing> 1 minute ago RUN /bin/sh -c apt-get install -y curl # bui… 14MB buildkit.dockerfile.v0
<missing> 1 minute ago RUN /bin/sh -c apt-get update # buildkit 18MB buildkit.dockerfile.v0
<missing> 3 weeks ago CMD ["bash"] 0B buildkit.dockerfile.v0
<missing> 3 weeks ago ADD file:… in / 74MB buildkit.dockerfile.v0Reading the columns
- SIZE is the amount of data that layer adds to the image. Add the column to get the image's total size.
- CREATED BY is the instruction that produced the layer. Long commands are cut off with
…. - IMAGE is the layer's ID. Many rows show
<missing>instead. - CREATED is how long ago the layer was built. Layers reused from the cache keep their original age.
The <missing> value does not mean something is broken. Under BuildKit, intermediate layers are not stored as separate local images, so Docker has no image ID to print for them. The same happens for layers pulled from a registry. The layer data is still in the image.
A size of 0B marks an instruction that changed no files. CMD, ENV, LABEL, and EXPOSE only change metadata. Notice the rm row in the example. It also shows 0B, but that does not mean it saved anything.
Spotting the waste
Compare the rows in the example:
apt-get updateadds 18 MB of package lists in/var/lib/apt/lists. No later step removes them.COPY data.tarstores the 48 MB archive in its own layer.tar -xfstores the extracted files in a second layer.rm /tmp/data.tarrecords a deletion but frees no space.
docker history tells you which layers are large and which instruction made them. It does not tell you which files are inside, or that an earlier layer's file was later deleted. For that you need dive, which a later chapter covers.
Showing full commands and custom columns
By default docker history truncates the CREATED BY column. Use --no-trunc to see each full instruction, which helps when a long RUN line hides the part you need.
docker history --no-trunc demo:wastefulThe --format option takes a Go template, or the word table to keep a header row. This command prints only the ID, size, and instruction:
docker history --format "table {{.ID}}\t{{.Size}}\t{{.CreatedBy}}" demo:wastefulOther available fields include .CreatedSince, .CreatedAt, and .Comment. Add -q to print only layer IDs, which is rarely useful because most are <missing>. Add -H=false to remove the header on the default table.
The improved version
Here the cleanup happens in the same layer that created the files, and a BuildKit bind mount means data.tar is never stored in any layer.
FROM debian:bookworm-slim
RUN apt-get update \
&& apt-get install -y --no-install-recommends curl \
&& rm -rf /var/lib/apt/lists/*
RUN --mount=type=bind,source=data.tar,target=/tmp/data.tar \
tar -xf /tmp/data.tar -C /optBuild it as demo:lean and run docker history demo:lean. You should see fewer rows. The package layer should be smaller because the lists were removed before the layer was saved. The tar layer holds only the extracted files.
Common mistakes
- Treating
0Bon armrow as proof of savings. - Assuming
<missing>means a failed build or a corrupt image. - Reading truncated instructions and guessing at the rest. Use
--no-trunc. - Expecting history to list files. It lists layers only.
Exercise
In docker history output, a RUN rm -rf /big-folder row shows 0B, yet the image is still large. Why, and which tool can show what remains?
Show answer
Each layer is stored separately. The rm only adds a deletion marker in a new layer. The files stay in the earlier layer that created them, so the image keeps their size. Dive can show the per-layer file changes and the wasted space. To prevent it, delete the files in the same RUN instruction that creates them.
Exercise
Write a docker history command for demo:wasteful that prints only the size and the full instruction, with a header row.
Show answer
docker history --no-trunc --format "table {{.Size}}\t{{.CreatedBy}}" demo:wasteful