Sub topic 10
Inspecting layers with docker history and docker image inspect
This chapter assumes you can run docker build, docker run, and docker pull, and that you can read a simple Dockerfile. You should also know that an image is a stack of read-only layers. Here you will learn to look inside an image and answer practical questions. Which step made it large? Which steps only added metadata? Which layers does it share with another image?
Two commands, two views
Docker gives you two main ways to look at an image's layers. They show different things.
| Command | What it shows | Best for |
|---|---|---|
| `docker history` | One row per build step, with the instruction text, creation time, and size | Finding which step made the image big |
| `docker image inspect` | The image's JSON metadata, including the list of filesystem layer digests | Comparing images and checking what is shared |
The key difference is that history is organized by Dockerfile instruction, while inspect lists only real filesystem layers. The two lists do not have the same length, and that is normal.
Reading docker history
Build a small image and then look at its history.
docker build -t myapp:dev .
docker history myapp:dev
docker image inspect --format '{{json .RootFS.Layers}}' myapp:devThe history output has these columns: IMAGE, CREATED, CREATED BY, SIZE, and COMMENT. Rows are listed newest first, so the last instruction in your Dockerfile is at the top and the base image is at the bottom.
Typical output for the Node example from earlier chapters looks like this. The IDs and times will differ on your machine.
IMAGE CREATED CREATED BY SIZE
a1b2c3d4e5f6 2 minutes ago CMD ["node" "dist/server.js"] 0B
<missing> 2 minutes ago RUN /bin/sh -c npm run build 4.1MB
<missing> 2 minutes ago COPY . . # buildkit 3.2MB
<missing> 3 minutes ago RUN /bin/sh -c npm ci 182MB
<missing> 3 minutes ago COPY package.json package-lock.json ./ 96kB
<missing> 3 minutes ago WORKDIR /app 0B
<missing> 3 weeks ago /bin/sh -c #(nop) CMD ["node"] 0B
<missing> 3 weeks ago /bin/sh -c #(nop) ADD file:... in / 74.8MBThree things to notice:
- Metadata-only instructions such as
CMDandWORKDIRshow0B. They change the image configuration but add no files. - Instructions that change the filesystem, such as
RUNandCOPY, show a nonzero size. That size is the data that step added to its layer, not the running total. - Most rows show
<missing>in theIMAGEcolumn. This is not an error. When you build with BuildKit, or pull an image, Docker does not keep an image ID for the intermediate steps. Only the top row, the final image, has a usable ID.
Useful flags
By default, Docker truncates long instructions. Two flags make the output easier to use.
docker history --no-trunc myapp:dev
docker history --human=false --format '{{.Size}}\t{{.CreatedBy}}' myapp:dev--no-trunc prints the full command of each step. --format takes a Go template, which lets you print only the columns you need. With --human=false, sizes appear as raw byte counts, which you can sort or sum in a script.
Reading docker image inspect
docker image inspect prints a large JSON document. You rarely want all of it. The --format flag selects one part.
docker image inspect --format '{{json .RootFS.Layers}}' myapp:dev
docker image inspect --format '{{.Os}}/{{.Architecture}}' myapp:dev
docker image inspect --format '{{json .Config.Cmd}}' myapp:dev
docker image inspect --format '{{.Size}}' myapp:devThe first command prints something like this:
["sha256:3f9a...","sha256:81c0...","sha256:d27e...","sha256:5b44..."]Each entry is the digest of one filesystem layer. The list runs from the bottom layer to the top. The order here is the reverse of docker history, which is a common source of confusion.
The .RootFS.Layers values are digests of the uncompressed layer contents. These are also called diff IDs. The digests you see when you pull from a registry are digests of the compressed layer, so they usually do not match the diff IDs. Both are valid, and they identify the same content in two different forms.
Why the two lists have different lengths
Count the rows in docker history, then count the entries in .RootFS.Layers. The history will have more rows. Rows with 0B size produce no filesystem layer, so they are absent from the inspect list.
The following diagram shows how instructions map to layers for a small Dockerfile.
A practical rule: if the size column in history is 0B, the step did not add a layer. There is one exception. A step can create a layer that is empty or nearly empty, for example RUN true, and some builders record such a step in history without storing a layer. When you need an exact count, trust .RootFS.Layers.
Finding the step that made the image large
The most common use of docker history is size hunting. Take the build from the layer size pitfalls chapter, where a file is downloaded in one step and deleted in the next.
FROM debian:bookworm-slim
RUN apt-get update && apt-get install -y --no-install-recommends curl ca-certificates \
&& rm -rf /var/lib/apt/lists/*
# Two separate RUN steps: the archive stays stored in the first layer.
RUN curl -fsSLO https://example.com/big.tar.gz
RUN tar -xzf big.tar.gz -C /opt && rm big.tar.gzThe history for this image shows the problem directly:
SIZE CREATED BY
48MB RUN /bin/sh -c tar -xzf big.tar.gz -C /opt && rm big.tar.gz
300MB RUN /bin/sh -c curl -fsSLO https://example.com/big.tar.gz
...The curl step is 300 MB because it stored the archive. The tar step adds the extracted files and removes the archive from the final view, but the 300 MB stays in the earlier layer. The image is the sum of both. Without history, the total size alone would not tell you which step to fix.
Checking whether layers are shared
Two images that start from the same base share the base layers, and Docker stores them once. You can verify that by comparing the layer lists.
docker image inspect --format '{{json .RootFS.Layers}}' app-a:latest
docker image inspect --format '{{json .RootFS.Layers}}' app-b:latestIf the first several digests are identical in both outputs, those layers are the same content. Docker keeps one copy on disk. This also explains why pulling a second image built on the same base is fast: the registry client skips layers it already has.
To see disk use across everything locally, run:
docker system df -vThe verbose output reports a shared size and a unique size for each image. Shared size counts layers used by more than one image.
Common errors and gotchas
- Reading history top to bottom as build order. History lists the newest step first. Inspect lists the oldest layer first.
- Treating
<missing>as a broken image. It only means no local image ID exists for that intermediate step. - Expecting the sizes to match the total. The history sizes are per step. Add them to get an approximate total. The
.Sizefield from inspect reports the uncompressed size, while a registry shows the compressed size, which is smaller. - Matching digests across a registry and a local machine. Compressed digests and diff IDs differ by design. Compare like with like.
- Expecting history on a pulled image to show your Dockerfile. History reflects what the image author recorded. Some images are built with tools that strip or rewrite these rows.
Summary of the workflow
- Run
docker historyto find the step that added the most data. - Check whether that step deletes files that an earlier step created.
- Run
docker image inspectto list layer digests when you need to compare images. - Rebuild after the fix and compare the new history to the old one.
Exercise
You run docker history on an image and see 9 rows, but docker image inspect --format '{{json .RootFS.Layers}}' lists only 6 digests. Is the image corrupt? What most likely explains the difference?
Show answer
The image is not corrupt. History has one row per instruction, including metadata-only instructions such as CMD, ENV, and WORKDIR, which show 0B and add no filesystem layer. Inspect lists only filesystem layers, so it has fewer entries. About 3 of the 9 rows are probably metadata-only.
Exercise
A build has RUN wget https://example.com/data.zip followed by RUN unzip data.zip -d /data && rm data.zip. The final image is larger than the extracted data. Which command shows you which step holds the zip file, and how would you fix the Dockerfile?
Show answer
Run docker history --no-trunc <image>. The wget row will show a size about equal to the zip file. To fix it, download, extract, and delete in one RUN step, for example RUN wget https://example.com/data.zip && unzip data.zip -d /data && rm data.zip. A multi-stage build, where the download happens in a throwaway stage, also works.