Sub topic 3
How layers and the build cache work
This chapter assumes you can write a simple Dockerfile and run docker build. It also assumes you know what a CI job is. Here you will learn how Docker decides which build steps to skip. That decision determines how long your CI builds take.
Layers
An image is a stack of layers. Each RUN, COPY and ADD instruction produces a layer that records the filesystem changes that step made. Instructions such as ENV, WORKDIR and CMD only add metadata. Layers are immutable and identified by a content hash, so different images can share them.
Two consequences follow:
- Deleting a file in a later layer hides it but does not shrink the image. The earlier layer still holds the bytes.
- A secret written to a layer stays in the image history, even if a later step removes it.
The cache rule
For each instruction, BuildKit (the build engine behind docker build) asks whether it already has a result for the same parent layer and the same instruction. If so, it reuses that result and prints CACHED.
How "same" is judged depends on the instruction:
| Instruction | Cache key |
|---|---|
| `RUN` | The command text and the parent layer. The command's output is not checked. |
| `COPY`, `ADD` | A checksum of the copied file contents. Timestamps are ignored. |
| `FROM` | The base image the tag resolves to. |
The most important rule is that one cache miss invalidates every later step. Order your instructions from least to most frequently changed.
FROM node:20-alpine AS build
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
RUN npm run buildThe manifests are copied first, so npm ci reruns only when package.json or package-lock.json changes. An edit to a source file invalidates the COPY . . step and the build step after it. The npm ci layer stays cached.
Common errors
- Copying everything first. With
COPY . .beforenpm ci, every source edit reinstalls all dependencies. - Splitting
apt-get updatefromapt-get install.RUNis keyed by its command text, so a cachedupdatelayer can go stale. Chain them in oneRUN. - Expecting the cache to notice external changes.
RUN git clone ...orRUN curl ...stays cached until the text changes, even if the remote content changed. Pin versions, or use--no-cachefor a one-off rebuild. - Sending a noisy build context. Files that change on every build, such as logs, invalidate
COPY . .. A.dockerignorefile prevents this.
See it locally
Build twice and compare the output:
docker build -t cache-demo .
touch src/index.js
docker build -t cache-demo .In the second build, the steps before COPY . . should report CACHED. The steps after it should run again. If npm ci reruns, something earlier in your Dockerfile or build context is changing.
Exercise
A Dockerfile runs COPY . . and then RUN npm ci. You change one line in a README file, and the build reinstalls all dependencies. Name two changes that avoid this.
Show answer
Copy only package.json and package-lock.json before running npm ci, then copy the rest of the source afterward. Also add README and other irrelevant files to .dockerignore so they do not change the copied content.