Image Layers and Build Cache
How Docker caches read-only filesystem layers and why COPY order determines build speed.
TL;DR
Every RUN, COPY, and ADD instruction in a Dockerfile creates an immutable layer. Once a layer's inputs change, Docker invalidates that layer and every instruction after it—so ordering instructions from least-frequently changed to most-frequently changed keeps builds fast.
Why it exists
Without layer caching, rebuilding a container after editing a single line of application code would re-download the base image, re-install OS packages, and re-run npm install from scratch. Layer caching lets Docker reuse unchanged work across builds.
Mental model
Think of a Dockerfile as a linear stack of checkpoints:
- Base image (
FROM node:22-alpine3.21) — changes rarely. - Dependency manifest (
COPY package.json ./+RUN npm install) — changes only when dependencies change. - Application source (
COPY server.mjs ./) — changes on almost every commit.
When Docker evaluates COPY, it checksums the copied files. If you write COPY . . before RUN npm install, editing server.mjs changes the COPY . . checksum, which busts the npm install layer below it. Splitting COPY package.json ./ from COPY server.mjs ./ isolates source edits to the final layer.
labs/docker-multistage-nextInspect the COPY order and two-stage Dockerfile in the minimal Node app lab.
docker build -t mt-multistage-next:lab labs/docker-multistage-nextDo it
Compare the slow order versus the cache-friendly order:
# Avoid — cache-busting order: any edit to server.mjs re-runs npm install
FROM node:22-alpine3.21
WORKDIR /app
COPY . .
RUN npm install --omit=dev
# Good — cache-friendly order: source edits skip npm install
FROM node:22-alpine3.21
WORKDIR /app
COPY package.json ./
RUN npm install --omit=dev --ignore-scripts
COPY server.mjs ./Run two consecutive builds on labs/docker-multistage-next to observe CACHED layers in BuildKit output:
docker build -t mt-multistage-next:lab labs/docker-multistage-next
# Edit server.mjs, then rebuild:
docker build -t mt-multistage-next:lab labs/docker-multistage-nextGotchas
- Missing
.dockerignore: Without.dockerignore,COPY . .sends localnode_modules,.git, and log files into the build context, slowing context transfer and causing surprise cache misses. - Unpinned base tags (
:latest): Even if yourDockerfileis unchanged, pulling a moving tag can silently change the base layer and invalidate everything below it. - Deleting files in a later
RUN: Removing a large temp file in a separateRUNinstruction does not shrink earlier layers; clean up in the sameRUNor use multi-stage builds.
Production checklist
- Order
COPYinstructions from least-frequently changed (package.json, lockfile) to most-frequently changed (source code). - Add a
.dockerignoreexcluding.git,node_modules,README.md, and local build artifacts. - Pin base image tags (for example
node:22-alpine3.21) instead of:latest. - Keep
RUNpackage installs and cache cleanups in the same layer or drop build tools in a multi-stage build.
Check yourself
Check Yourself
1. You edit server.mjs in a project whose Dockerfile runs COPY package.json ./, then RUN npm install, then COPY server.mjs ./. Which layers rebuild?
2. Why is a .dockerignore file important even when you only COPY specific files?