Skip to main content

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.

Bash
Layer 4: COPY . . application source
Layer 3: RUN npm ci node_modules
Layer 2: COPY package.json ./ manifest
Layer 1: FROM node:20-alpine base OS and runtime

At 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

Bash
docker history payment-api:v3.1.0 # layers and sizes
docker image inspect payment-api:v3.1.0
docker images --digests
docker image prune # dangling images only
docker image prune -a --filter "until=48h" # unused images older than 48h

Naming, tags, and digests

Bash
# [registry/][namespace/]name[:tag][@digest]
docker tag payment-api:v3.1.0 registry.example.com/payment-api:v3.1.0
docker pull nginx@sha256:<digest> # exact content, immutable

An 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.

Bash
docker images -f dangling=true
docker image prune -f

Troubleshooting

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 Mistake

Using :latest in production. Tags are mutable, so deployments become unpredictable.

Security

Base 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.

Found a mistake, an outdated step or a broken link?