Sub topic 6
Ordering instructions to keep the cache warm
This chapter assumes you can read a Dockerfile and know that a cache miss on one step forces every later step to rebuild. Here you will use that rule to order instructions so that routine edits rebuild as little as possible.
The ordering rule
Put the instructions that change least at the top. Put the ones that change most at the bottom. A source file changes on almost every commit. A dependency manifest changes occasionally. The base image changes rarely. Your Dockerfile should follow that same order.
A common slow pattern copies everything first:
FROM node:20-slim
WORKDIR /app
COPY . .
RUN npm ci
RUN npm run build
CMD ["node", "dist/server.js"]Any edit to any file changes the COPY . . input. That step misses the cache, so npm ci and the build rebuild too, even though the dependencies are unchanged.
Split the copy
Copy only the dependency manifests, install, and then copy the rest:
FROM node:20-slim
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
RUN npm run build
CMD ["node", "dist/server.js"]The first COPY depends only on the two manifest files. While they stay the same, COPY and npm ci are cache hits. An edit to src/index.js misses the cache only at COPY . ., so only that step and the build step after it run again.
Apply the same idea to each ecosystem
The pattern is the same everywhere: copy the file that describes dependencies, install, then copy the code.
| Ecosystem | Copy first | Install step |
|---|---|---|
| Node.js | `package.json`, `package-lock.json` | `npm ci` |
| Python | `requirements.txt` | `pip install -r requirements.txt` |
| Go | `go.mod`, `go.sum` | `go mod download` |
Keep package installs together
Within a single RUN, put update, install, and cleanup in one command:
FROM debian:bookworm-slim
RUN apt-get update \
&& apt-get install -y --no-install-recommends curl \
&& rm -rf /var/lib/apt/lists/*If apt-get update were its own RUN, its layer could stay cached for months. A later apt-get install in a separate step would then use a stale package index and might fail or install old versions. Combining them ties the index to the install.
Common errors
- Copying everything early.
COPY . .before the install step wipes out dependency caching. - Putting volatile instructions high. An
ARGfor a build number, or aRUNthat writes a timestamp, near the top invalidates every step after it. Move such steps as low as possible. - Assuming a cache hit means fresh content. Docker does not check whether a
RUNcommand would give a different result today. A cachedRUN apt-get updatestays cached until its text or earlier inputs change. To force a rebuild, usedocker build --no-cache. - Putting rarely changing steps after frequent ones. Installing system packages after copying source means each source edit reinstalls them.
Exercise
A Dockerfile has COPY . . followed by RUN pip install -r requirements.txt. You change one line in a Python source file. Which steps rebuild, and how would you change the Dockerfile so the install stays cached?
Show answer
COPY . . misses the cache because the source file changed, so pip install reruns as well. Copy requirements.txt alone first (COPY requirements.txt .), run pip install -r requirements.txt, and then COPY . .. A source edit then rebuilds only the final copy step and anything after it.