Sub topic 3
Ordering instructions for the build cache
This chapter assumes you know that an image is a stack of layers and that you can read a simple Dockerfile. It covers how Docker decides to reuse a layer, and how to order instructions so most rebuilds finish in seconds.
How the cache decides
Each instruction that changes the filesystem produces a layer. Before running an instruction, Docker checks whether it already has a layer built from the same parent layer with the same instruction. If so, it reuses that layer and prints CACHED.
- For
RUN, the check compares the instruction text. Docker does not look at what the command downloads, soRUN apt-get updatestays cached even when the package index has changed upstream. - For
COPYandADD, the check compares a checksum of the copied files' contents (and metadata such as permissions). A change to any copied file invalidates the layer. - For
ENV,ARG,WORKDIRand similar instructions, the check compares the instruction text and values.
The rule that shapes everything else: when one layer is invalidated, every layer after it is rebuilt. Docker cannot assume later layers still make sense on top of a changed parent.
A common mistake
This Dockerfile copies everything first and installs dependencies second.
FROM node:20-slim
WORKDIR /app
COPY . .
RUN npm ci
RUN npm run build
CMD ["node", "dist/index.js"]Editing one line of source changes the checksum of COPY . .. That invalidates the layer, so npm ci runs again and re-downloads every dependency, even though package.json did not change.
Order from least to most frequently changing
Copy only the dependency manifests, install, and then copy the rest.
FROM node:20-slim
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
CMD ["node", "dist/index.js"]Now a source edit invalidates only the second COPY and the build step. The npm ci layer is reused until package.json or package-lock.json changes. The same pattern applies to other ecosystems:
| Ecosystem | Copy first | Install step |
|---|---|---|
| Node.js | `package*.json` | `npm ci` |
| Python | `requirements.txt` | `pip install -r requirements.txt` |
| Go | `go.mod go.sum` | `go mod download` |
Watching the cache work
Build twice, edit a source file, and build again. With BuildKit, the output marks each step.
docker build -t myapp:dev .
echo "// touch" >> src/index.js
docker build -t myapp:dev .On the third command, the steps up to RUN npm ci should show CACHED, and the steps after the source COPY should run. If npm ci runs again, something earlier changed.
Other ways to lose the cache
- Unstable context. A changing file in the build context, such as a log or
.gitdata, invalidatesCOPY . .on every build. The next chapter shows how.dockerignoreprevents this. - Changing values early. An
ARGorENVholding a build timestamp or commit hash, placed near the top, invalidates everything after it. Put such values as late as possible. - Stale package indexes. Because
RUNis cached by its text, runapt-get updateandapt-get installin the sameRUNinstruction. Otherwise a cachedupdatelayer can go stale, and a laterinstallmay fail. - Forcing a rebuild. Use
docker build --no-cacheto ignore the cache, or--pullto check for a newer base image.
Exercise
A Dockerfile has COPY . . followed by RUN pip install -r requirements.txt. You change one line in app.py, and pip reinstalls every package. How do you fix the ordering, and why does the fix work?
Show answer
Copy requirements.txt alone, run pip install -r requirements.txt, and only then run COPY . .. The install layer's cache key now depends only on requirements.txt. A change to app.py invalidates just the later COPY layer, so the pip layer is reused.