Skip to content

Lesson 5 of 9 · 20m

Layer Caching and Dockerfile Discipline

BuildKit caches by instruction and input hash — order your Dockerfile for cache hits, and rebuilds drop from minutes to seconds.

The cache rule is one sentence: BuildKit reuses a layer only if the instruction and everything it depends on is byte-identical, and once one layer misses, every layer after it rebuilds. Dockerfile authorship is therefore mostly instruction ordering — put the things that change rarely first and the things that change constantly last.

The classic mistake, and the fix

# bad: source changes invalidate dependency install — every commit is a full npm install
FROM node:22-bookworm-slim
WORKDIR /app
COPY . .
RUN npm ci
CMD ["node", "server.js"]
# good: deps are cached until the lockfile changes
FROM node:22-bookworm-slim
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci --omit=dev
COPY . .
CMD ["node", "server.js"]

With the bad version, every one-line source edit reruns a 90-second npm ci. With the good version, dependency install rebuilds only when package-lock.json actually changes — maybe weekly. On a team of ten that's hours of CI time recovered per day.

Cache mounts: caching what layers can't

Some caches should survive even when the layer rebuilds — package manager downloads, compiler artifacts. BuildKit cache mounts attach a persistent cache directory to a single RUN:

# syntax=docker/dockerfile:1
RUN --mount=type=cache,target=/root/.cache/pip     pip install -r requirements.txt

Now pip re-downloads nothing across builds even when requirements change slightly. Same trick works for npm (/root/.npm), Go (/go/pkg/mod), cargo. Note the # syntax pin — cache mounts require the dockerfile 1.x frontend, which is default on modern Docker but worth declaring.

.dockerignore is a cache tool, not housekeeping

COPY . . hashes the entire build context. Without .dockerignore that includes .git, node_modules, test fixtures, and the 2GB of sample data someone left in docs/. Every change to any of those invalidates the copy layer. A disciplined .dockerignore:

.git
node_modules
**/*.md
tests/
*.log

It also shrinks context upload — on a remote builder or CI, the difference between a 4MB context and a 900MB context is most of your build time before the first instruction runs.

Field notes

  • RUN apt-get update && apt-get install must be one instruction. Split into two and the cached update layer goes stale while install hits the cache — you ship ancient package indexes and wonder why versions drift.
  • Combine related mutations into one RUN (with && and line continuations) only within a stage of one logical step; over-combining destroys cache granularity.
  • --no-cache is a debugging tool, not a lifestyle. If builds are wrong without it, your cache invalidation logic is lying somewhere upstream.
  • Remote builders (buildkitd, depot, GitHub Actions cache backends) need --cache-to/--cache-from configured — local cache doesn't cross machines by itself.

Was this lesson helpful?

Sign in to rate