Sub topic 2
Why CI builds start cold
This chapter assumes you can write a Dockerfile, know that Docker reuses unchanged layers, and understand CI jobs and runners. It explains why that reuse usually disappears in CI. The next chapters fix it.
The cache lives on one machine
Docker stores its layer cache in the local build cache of the builder that ran the build. On your laptop, the first build fills that cache. Later builds reuse every step whose inputs have not changed.
The cache is local state. It sits on one disk, managed by one Docker daemon or BuildKit instance. Another machine cannot see it.
What "ephemeral" means for a runner
Most CI services run each job on a fresh, short-lived virtual machine or container. This is called an ephemeral runner. When the job ends, the runner is destroyed along with its disk.
This has two effects on Docker builds:
- A new runner starts with an empty build cache.
- Even a runner that survives may be shared, so you cannot assume the next job lands on it.
Every build therefore begins cold. Each instruction runs from scratch, even if the Dockerfile and the files it copies are identical to the last run.
A cold build in practice
Take a small Node.js project with this Dockerfile:
FROM node:20-slim
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
RUN npm run build
CMD ["node", "dist/index.js"]The file is already ordered well for caching. Locally, npm ci is skipped unless a manifest changes. Now run it in a basic workflow:
name: build
on:
push:
branches: [main]
pull_request:
jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- uses: actions/checkout@v4
- uses: docker/setup-buildx-action@v3
- uses: docker/build-push-action@v6
with:
context: .
push: false
tags: myapp:ciEach run does the following:
- Pulls the
node:20-slimbase image. - Downloads and installs every npm dependency.
- Compiles the application.
None of this changes when you fix a typo in a README, but the job still does all of it. The COPY . . layer would legitimately be rebuilt after a source change. The npm ci layer should not be.
What a cold build costs
The costs grow with the project:
- Time. Dependency installs and compilation often dominate build time. Developers wait longer for feedback.
- Money. CI minutes are billed, and slow builds multiply across every push and pull request.
- Network load. Each run re-downloads base images and packages, which can hit registry rate limits or package mirror outages.
- Flakiness. More downloads mean more chances for a transient network failure.
A well-ordered Dockerfile does not solve this alone. Ordering decides which layers could be reused. A cache that survives between runs decides whether they are.
The shape of the fix
The cache has to leave the runner before it is destroyed and come back when the next runner starts. That means storing it somewhere shared and durable:
- an image registry,
- the GitHub Actions cache service,
- or an S3 bucket.
BuildKit, the build engine behind docker buildx, can export its cache to these places and import it again. The following chapters cover the options, builder setup, and each backend.
Exercise
A CI job builds an image with a correctly ordered Dockerfile. A teammate changes only a comment in the README. The job still reruns npm ci on every push. Name the cause and the general fix.
Show answer
The runner is ephemeral, so its local layer cache is empty at the start of every job and the Dockerfile ordering cannot help. The fix is to export the BuildKit cache to a shared remote backend (registry, gha, or S3) at the end of a build and import it at the start of the next.