Sub topic 10
Inspecting layers with docker history and docker image inspect
This chapter assumes you can run docker build and docker run, read a simple Dockerfile, and know that an image is a stack of read-only layers. Here you will look at those layers directly instead of inferring them from the Dockerfile.
Why inspect layers
A Dockerfile shows what you asked for. The built image shows what you got. Two commands answer the questions that matter when an image is larger than expected or a cache hit did not happen: which step added the bytes, and which filesystem layers make up the image.
Listing steps with docker history
Build an image, then list its steps:
docker build -t myapp:dev .
docker history myapp:devdocker history prints one row per step, newest first. Each row has these columns:
IMAGE: the layer ID, or<missing>for steps built on another machine or pulled from a registry.<missing>is normal and does not indicate an error.CREATED BY: the instruction that produced the step.SIZE: the bytes the step added to the filesystem.
Steps that only add metadata, such as CMD, ENV, or WORKDIR on an existing directory, show a size of 0B. Steps that write files, such as COPY and RUN, show a real size. This is the quickest way to see which instructions create filesystem layers.
Use --no-trunc to see the full instruction text, and --format to shape the output:
docker history --no-trunc myapp:dev
docker history --format '{{.Size}}\t{{.CreatedBy}}' myapp:devReading the layer digests with docker image inspect
docker history lists steps. docker image inspect lists the filesystem layers themselves, identified by digest:
docker image inspect --format '{{json .RootFS.Layers}}' myapp:devThe output is a JSON array of sha256:... values, ordered from the base layer to the top. Each digest is a hash of that layer's contents, so identical content always gives the same digest. Images that share a base image share those leading digests, and Docker stores them once.
The array is shorter than the history. Zero-size metadata steps have no filesystem layer, so they appear in docker history but not in RootFS.Layers.
Finding where the size comes from
Use docker history to find the large rows, then match them to Dockerfile lines. Suppose a build shows this:
| SIZE | CREATED BY |
|---|---|
| 0B | CMD ["node" "dist/server.js"] |
| 48MB | RUN npm run build |
| 180MB | COPY . . |
| 95MB | RUN npm ci |
The COPY . . row is large. That points to the build context, and a .dockerignore file is the likely fix. If a file was deleted in a later step but a large row remains earlier, the deletion did not shrink the image, because the earlier layer still stores the file.
Comparing two builds
To check whether a change reused the cache, inspect both images and compare the digest lists. Layers that appear in both lists were reused or were identical. The first digest that differs marks where the cache stopped matching, and every digest after it differs too.
docker image inspect --format '{{range .RootFS.Layers}}{{println .}}{{end}}' myapp:v1 > v1.txt
docker image inspect --format '{{range .RootFS.Layers}}{{println .}}{{end}}' myapp:v2 > v2.txt
diff v1.txt v2.txtExercise
You run docker history on an image. A RUN rm -rf /var/cache/big step shows 0B, yet the image is still large. Why, and which command shows the layers that hold the data?
Show answer
The rm step only hides the files in the final filesystem view, so it adds almost no bytes. The earlier layer that created the files still stores them. Run docker history and look for the large earlier row, or run docker image inspect --format '{{json .RootFS.Layers}}' to list the layer digests.