Images and Layers
How Docker's union filesystem makes images fast, small, and shareable
Skills taught: docker.layers, docker.caching, docker.base-images, docker.alpine
How image layers work
A Docker image is not a single monolithic file — it's a stack of read-only layers. Each instruction in a Dockerfile creates a new layer. When you run a container, Docker adds one thin writable layer on top. This design means: layers are cached and reused across images; if 10 images share the same base OS layer, that layer is stored once on disk; and downloading a new image only transfers the layers you don't already have.
# See the layers of any image
docker history node:20-alpine
# IMAGE CREATED CREATED BY SIZE
# 1d46ed2a75d7 3 weeks ago CMD ["node"] 0B
# <missing> 3 weeks ago ENV NODE_VERSION=20.11.0 0B
# <missing> 3 weeks ago RUN /bin/sh -c apk add --no-… 44.3MB
# <missing> 3 weeks ago RUN /bin/sh -c apk update 3.1MB
# <missing> 3 weeks ago /bin/sh -c #(nop) ADD file:… 7.38MB
# Each line = one layer. Sizes add up to the total image size.
# Show detailed image info including all layers
docker inspect node:20-alpine | grep -A5 'Layers'
Choosing a base image
The first line of every Dockerfile is FROM <base-image>. Your choice has a huge impact on image size, security surface, and build speed. The most important variants to know:
- node:20 — full Debian-based image. ~1 GB. Has everything, but huge. Avoid in production.
- node:20-slim — stripped Debian. ~220 MB. Good balance for Node apps.
- node:20-alpine — Alpine Linux base. ~55 MB. Minimal, fast to pull, preferred for production. Uses musl libc instead of glibc — rare compatibility issues.
- scratch — empty base (literally nothing). Used for Go or Rust apps that compile to a fully static binary.
docker pull node:20 && docker images node:20
# node 20 ~1.1GB
docker pull node:20-slim && docker images node:20-slim
# node 20-slim ~220MB
docker pull node:20-alpine && docker images node:20-alpine
# node 20-alpine ~55MB
# Rule of thumb: start with alpine, switch to slim if you hit issues
Layer caching — the build speed secret
Docker caches each layer. When you rebuild an image, Docker reuses cached layers until it finds one that changed — then rebuilds from that point down. This is why the order of instructions in your Dockerfile matters enormously for build speed.
# ❌ SLOW — copies code first, so npm install reruns on every code change
FROM node:20-alpine
COPY . . # copies ALL files — cache busted on any change
RUN npm install # reruns every time any file changes
CMD ["node", "app.js"]
# ✅ FAST — copies package.json first; npm install only reruns when deps change
FROM node:20-alpine
WORKDIR /app
COPY package*.json ./ # only package files — cache busted only when deps change
RUN npm install # cached unless package.json changed
COPY . . # copy source code last
CMD ["node", "app.js"]
# With the fast version:
# First build: all layers run (slow)
# Code change only: only last COPY . . and CMD rerun (fast)
# Dep change: npm install and below rerun
Tip: Pin your base image versions. Always use node:20-alpine not node:alpine (which is 'latest alpine' and changes over time). Pinning ensures reproducible builds — the same image is built months later. For critical production systems, pin to the full digest: node:20-alpine@sha256:...
Frequently Asked Questions
What is .dockerignore?
Like .gitignore but for Docker builds. A .dockerignore file tells Docker which files to exclude from the build context (what gets sent to the Docker daemon). Always exclude node_modules, .git, .env, and large binary files. Example: add 'node_modules' and '.git' and '*.log' to .dockerignore to dramatically speed up builds.