Skip to main content
PProfileMatch
← Path

Docker · Writing a Good Dockerfile: Caching, Multi-Stage Builds and Non-Root

0 XP
  1. 1 current stop: learning
  2. 2 upcoming
  3. 3 upcoming
  4. 4 upcoming
  5. 5 upcoming

Finish after stop 5

Stop 1 of 5 · Workshop: sorting tools by how often you grab them

Trick to keep in mind+3 XP

Slow and stable first, fast and fickle last. Docker rebuilds from the first change downward.

A cached layer is reused only if its own inputs are unchanged and every layer above it was also reused. Once one layer changes, everything after it rebuilds. So order steps by how rarely they change. Dependency manifests change occasionally; your source changes constantly, so it belongs lower down.

COPY package.json package-lock.json ./
RUN npm ci
COPY src ./src

Read it once, then a question tests whether the trick stuck.