Image History

Difficulty: Intermediate

Question

How do you see the layers of an image and the instructions that created them, and how would you use that to make an image smaller?

Answer

Run docker history <image>. It lists the image's layers from newest to oldest, showing for each one the instruction that created it (CREATED BY), when it was created and how much it added (SIZE). Add --no-trunc to see full commands. It is the quickest way to find which Dockerfile step makes an image big, and to see roughly how a third-party image was built.

Reading the output

  • Top row is the newest layer, usually your CMD or ENTRYPOINT; the bottom rows are the base image.
  • SIZE 0B rows come from metadata-only instructions (ENV, EXPOSE, CMD, LABEL, WORKDIR). Only RUN, COPY and ADD add content.
  • IMAGE <missing> is normal. Layers pulled from a registry or built with BuildKit do not have intermediate image IDs on your machine; it does not mean anything is broken.
  • COMMENT buildkit.dockerfile.v0 marks layers built by BuildKit.
  • For multi-stage builds, only the final stage's layers appear. The builder stages are not part of the image, which is exactly why multi-stage builds shrink images.

Using history to slim an image

  1. Sort the big rows. A RUN npm install layer at 600 MB, or a COPY . . that is far larger than your source, points straight at the problem.
  2. Look for cleanup in a separate RUN. A row that removes caches with 0B size means the files are still in the earlier layer; merge install and cleanup into one RUN.
  3. A huge COPY . . usually means a missing .dockerignore (node_modules, .git or build output being copied).
  4. Large toolchain layers (compilers, build-essential, dev dependencies) suggest moving to a multi-stage build that copies only the final artefact into a slim or distroless base.
  5. Check the base image rows. Switching from a full python:3.12 to python:3.12-slim often saves more than every other change combined.

For deeper analysis, dive shows each layer's file tree and wasted space, and docker image inspect -f '{{json .RootFS.Layers}}' lists the layer digests.

Limits and security notes

  • History is metadata written by the builder, not proof. It can be missing (squashed or imported images show a single layer) or simply uninformative.
  • It shows commands, not the files that COPY brought in.
  • It can leak information: build-arg values used in RUN commands and any secrets passed as ENV show up in --no-trunc output. Anyone who can pull the image can read them, which is why secrets belong in RUN --mount=type=secret, not in args or env.
  • For supply-chain needs, rely on SBOMs and signed provenance attestations rather than docker history.
docker history of a Node.js image, newest first. Reading sizes per instruction shows where the weight comes from.
  1. CMD ["node", "dist/server.js"] 0B: Metadata only, adds no files.
  2. COPY . . 48MB: Too large: probably missing a .dockerignore.
  3. RUN npm ci 310MB: Includes devDependencies; use a multi-stage build.
  4. COPY package*.json ./ 410kB: Small layer that keys the dependency cache.
  5. Base image node:22 about 1.1GB: Switch to node:22-slim or alpine to cut most of this.

Code examples

Inspecting layers and sizes

# Default view, newest layer first
docker history orders-api:2.3

# Full commands, human-readable sizes, custom columns
docker history --no-trunc --format 'table {{.Size}}\t{{.CreatedBy}}' orders-api:2.3

# Layer digests from the image config
docker image inspect -f '{{json .RootFS.Layers}}' orders-api:2.3

The SIZE column immediately shows that npm ci and COPY . . are the heavy layers worth optimising.

Fix suggested by the history

# Build stage: full toolchain and devDependencies
FROM node:22 AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build && npm prune --omit=dev

# Runtime stage: only what production needs
FROM node:22-slim
WORKDIR /app
COPY --from=build /app/node_modules ./node_modules
COPY --from=build /app/dist ./dist
CMD ["node", "dist/server.js"]

After this change, docker history of the final image shows only the slim base plus two small COPY layers.

Key points

  • docker history lists layers newest first with the creating instruction and size.
  • 0B rows are metadata instructions; only RUN, COPY and ADD add content.
  • <missing> image IDs are normal for pulled or BuildKit-built layers.
  • Only the final stage of a multi-stage build appears in the history.
  • History can expose build args and env values, so never pass secrets that way.

Common follow-up questions

  • Why does the IMAGE column show <missing> for most layers?
  • How do multi-stage builds change what docker history shows?
  • What tool would you use to see which files each layer added?
  • Can docker history reveal secrets? How do you avoid that?

Interview tips

  • Turn it into an optimisation story: read sizes, find the heavy layer, fix the Dockerfile.
  • Explain <missing> calmly; candidates who think it is an error lose points.
  • Mention the secrets leak risk to show security awareness.

Concepts covered

docker history, Image layers, Layer size, Image optimisation, BuildKit

More Docker & Containers questions