Use it to make images smaller, faster to pull and easier to patch.
Container Images and Layers
A container image is an ordered stack of read-only layers (each a filesystem diff) plus a config (entrypoint, env, user). Containers add a thin writable layer on top using a union/overlay filesystem.
1. docker history <img> — layer sizes and commands.
2. Large layers → combine steps and clean caches in the same RUN.
3. dive <img> to explore (if installed).
4. Multi-stage builds to drop build tools.
5. Rebuild and compare size.
base layer → added layers → image config → container writable layer
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 How to verify Verify boundary
Compare image size with docker image ls and docker history. Confirm the effective entrypoint and user in the config. After a rebuild, check the digest changed before assuming the new image runs.
Step 2 Inspecting images Verify boundary
docker image inspect nginx:1.27 --format '{{json .RootFS.Layers}}'
docker history nginx:1.27
docker image ls --digests
dive nginx:1.27 # browse layers and wasted space
Each Dockerfile instruction that changes files creates a layer; layers are shared between images and cached during builds. Keep images small: multi-stage builds, minimal bases (distroless, alpine, ubi-micro), clean package caches in the same RUN step, and put rarely changing steps first for better caching.
Step 3 Common failures Narrow the fault
Copying secrets in one layer and deleting them in the next still ships them. Large layers from package caches bloat images. A container that writes logs or data to its own filesystem fills the writable layer and loses the data on recreation.
Each Dockerfile instruction can create a layer; files deleted in a later layer still exist in earlier ones (and in the image size). Order instructions so rarely changing layers come first for caching.
docker history <img>— layers and sizesdocker ps -a— containers and exit codesdocker logs <c>— container outputdocker inspect <c>— config, mounts, network, state
ReferenceWhat it is1 section · depth on demand
How it works
The storage driver (normally overlay2) stacks layers with overlayfs: lower read-only layers and one upper writable layer. Deleting a file in a later layer only hides it, so secrets copied in an early layer stay in the image. Multi-stage builds keep build tools out of the final image.
ReferenceImportant terms & boundaries2 sections · depth on demand
In depth
An OCI image is three kinds of content-addressed objects in a registry:
1. Manifest — lists the config blob and the ordered layer blobs, each by digest.
2. Config — entrypoint, cmd, env, user, working directory, exposed ports and the rootfs.diff_ids of the layers.
3. Layers — tar archives of filesystem changes, usually gzip- or zstd-compressed.
A multi-architecture tag points to an image index, which lists one manifest per platform.
Why layer order matters
Each RUN, COPY and ADD in a Dockerfile produces one layer. A file deleted in a later layer is only hidden by a whiteout entry; its bytes are still pulled and stored. Combine install and cleanup in one RUN, or use a multi-stage build so build-only files never reach the final image.
Worked check — where does the size come from?
docker history --no-trunc --format '{{.Size}}\t{{.CreatedBy}}' app:1.4 | head
# 312MB RUN apt-get update && apt-get install -y build-essential ...
# 4.1kB COPY . /src
A large RUN layer with compilers in a runtime image is the usual cause of oversized images.
Shared layers
Layers with the same digest are stored once per node and pulled once. Images built from the same base share those layers, so updating the base image in one place speeds up pulls everywhere; changing an early layer forces every later layer to be rebuilt and re-pulled.
Common mistakes
- Large images with build tools and secrets in layers.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Terms & aliases
Hover a linked term for a quick definition; click it to open its glossary entry. Aliases are other names that find this Topic in search.