Docker Image
A Docker image is a read-only, layered template used to create containers. Each filesystem-changing Dockerfile instruction adds an immutable layer, and layers are cached and shared across images and builds.
Layers, not one big file
An image is a stack of layers identified by content hashes, plus a config and manifest. Because layers are immutable and shared, ten containers from one image store the base layers once, and a rebuild only recreates layers that changed.
Layer 4: COPY . . application sourceLayer 3: RUN npm ci node_modulesLayer 2: COPY package.json ./ manifestLayer 1: FROM node:20-alpine base OS and runtimeAt run time OverlayFS merges the layers into one view and adds a writable layer per container. Writes to a file from a lower layer trigger copy-on-write: the file is copied up and modified there, leaving the image layer untouched.
Inspecting an image
docker history payment-api:v3.1.0 # layers and sizesdocker image inspect payment-api:v3.1.0docker images --digestsdocker image prune # dangling images onlydocker image prune -a --filter "until=48h" # unused images older than 48hNaming, tags, and digests
# [registry/][namespace/]name[:tag][@digest]docker tag payment-api:v3.1.0 registry.example.com/payment-api:v3.1.0docker pull nginx@sha256:<digest> # exact content, immutableAn image ID identifies the local image config, while a digest identifies the manifest in a registry. Tags can move, digests cannot.
Dangling images
Rebuilding with the same tag leaves the previous image untagged as <none>. They waste disk on build machines.
docker images -f dangling=truedocker image prune -fTroubleshooting
| Problem | Cause | Fix |
|---|---|---|
pull access denied |
Not logged in | docker login |
image is being used by stopped container |
Container still references it | docker rm first |
| Disk full on builder | Old images and cache | Prune on a schedule |
| Different bytes for same tag | Tag was re-pushed | Pin by digest |
Common MistakeUsing
:latestin production. Tags are mutable, so deployments become unpredictable.
SecurityBase images carry OS package CVEs. Scan with Trivy or similar before pushing and fail CI on critical findings.
Frequently Asked Questions
Why do images use layers?
Layers let Docker cache and reuse filesystem changes across builds and share them between images and containers. Only changed layers are rebuilt, stored, or transferred.
What is the classic image-size mistake?
Installing build dependencies and deleting them in a separate RUN. The earlier layer still contains the files, so the image does not shrink. Combine install and cleanup in one RUN, or use a multi-stage build.
What is a dangling image?
An image with no tag, usually the previous build of a tag that was rebuilt. It still uses disk until pruned.