Sub topic 4
Layers and build caching: ordering instructions for fast rebuilds
This chapter assumes you can write a basic Dockerfile, run docker build, and know what a build context is. You will learn how Docker builds an image from layers, when it reuses them, and how to order instructions so most rebuilds finish in seconds.
How layers work
Each RUN, COPY, and ADD instruction produces a layer. A layer is a stored set of filesystem changes on top of the previous layer. An image is the stack of these layers.
Before running an instruction, Docker checks whether it already has a layer built from the same parent layer and the same instruction. If so, it reuses that layer and prints CACHED. This is the build cache.
The cache rules that matter most:
- For
RUN, Docker compares the command text. It does not check whether the command's result would be different today. - For
COPYandADD, Docker compares the checksums of the files being copied. - Once one layer is invalidated, every layer after it is rebuilt, even if its own instruction did not change.
The last rule drives the whole strategy. Put things that change rarely near the top and things that change often near the bottom.
A slow ordering
Consider this Dockerfile.
FROM node:22-alpine
WORKDIR /app
COPY . .
RUN npm ci --omit=dev
CMD ["node", "server.js"]COPY . . copies the whole project, including your source code. Edit one line in server.js and the checksum of that copy changes. The cache is invalidated at that step, so npm ci runs again and reinstalls every dependency, even though package.json did not change.
A fast ordering
Copy only the dependency manifests first, install, and then copy the rest.
FROM node:22-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev
COPY . .
ENV NODE_ENV=production
EXPOSE 3000
USER node
CMD ["node", "server.js"]Now the npm ci layer depends only on package.json and package-lock.json. When you edit server.js, Docker reuses everything through RUN npm ci and rebuilds only the COPY . . layer and what follows. A rebuild drops from a minute of downloading packages to a second or two.
Rebuild after changing source and look for CACHED next to the install step:
docker build -t myapp:1.0 .Common errors
- Stale
RUNoutput. A command such asRUN apt-get updateis cached by its text. A later build can reuse an old package index. To force a fresh run, usedocker build --no-cache, or change the command. - Copying everything too early. Any
COPY . .placed before an expensive step makes that step re-run on every source edit. - Splitting update and install. Put
apt-get updateandapt-get installin oneRUNinstruction. If they are separate, the cachedupdatelayer can go stale whileinstallreruns. - Changing an early instruction. Editing an
ENVorARGline near the top invalidates every layer after it.
Summary of the ordering rule
| Position | Put here | Examples |
|---|---|---|
| Top | Rarely changes | Base image, `WORKDIR`, system packages |
| Middle | Changes with dependencies | `COPY package*.json`, `RUN npm ci` |
| Bottom | Changes on every edit | `COPY . .`, build steps |
Exercise
You edit only a comment in server.js. In the slow Dockerfile, which steps rerun? In the fast Dockerfile, which steps rerun?
Show answer
In the slow Dockerfile, COPY . . is invalidated because the file checksum changed, so RUN npm ci and everything after it rerun. In the fast Dockerfile, the package*.json copy and npm ci stay cached. Only COPY . . and the instructions after it rerun.
Exercise
Why can a cached RUN apt-get update layer cause problems, and how do you force a fresh build?
Show answer
Docker caches a RUN layer by its command text, not by what the command would return now, so the package index can be out of date. Run docker build --no-cache, or change the command text, to force it to run again.