CI/CD3 min read· 3 practice problems

Docker build cache not used (npm ci reruns): causes and fixes (COPY order, .dockerignore, CI cache)

If changing only source files makes npm ci or pip install rerun on every Docker build, check COPY order, .dockerignore, ARG placement and the missing cache on CI runners, then fix it by copying the lockfile first and using a buildx cache.

On this page
  1. Short answer: the cache breaks at a step before the dependency install
  2. Diagnosis: find where CACHED stops
  3. Cause 1: layer order — copy the lockfile first
  4. Cause 2: no .dockerignore
  5. Cause 3: where the ARG sits
  6. Cause 4: CI runners have no local cache
  7. What not to do
  8. The order to work in
  9. Quick diagnostic checklist

Short answer: the cache breaks at a step before the dependency install

Docker compares the Dockerfile with its cache one step at a time from the top, and once a step changes, every step below it runs again. If editing one line of the README makes npm ci or pip install start from scratch, the cause is almost always one of four:

  1. COPY . . comes before the dependency install
  2. There is no .dockerignore, so .git and node_modules end up in the build context
  3. An ARG that changes every build is declared above the install step
  4. The CI runner has no cache from earlier builds

Diagnosis: find where CACHED stops

docker build --progress=plain -t checkout:dev .
#5 [2/5] WORKDIR /app
#5 CACHED

#6 [3/5] COPY . .
#6 DONE 0.8s

#7 [4/5] RUN npm ci
#7 11.92 added 812 packages, and audited 813 packages in 11s
#7 DONE 12.6s

The first step that is no longer CACHED is the culprit. If it breaks at COPY . . as above, it is cause 1 or 2; if the install step is the first to miss, cause 3; if everything is CACHED locally but reruns in CI, cause 4. The legacy builder (without BuildKit) prints ---> Using cache for steps it reuses.

Cause 1: layer order — copy the lockfile first

FROM node:20-slim
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
RUN npm run build

Now a source-only change leaves npm ci CACHED, and dependencies are reinstalled only when the lockfile changes. For Python the order is COPY requirements.txt . → RUN pip install -r requirements.txt → COPY . ..

Cause 2: no .dockerignore

Suspect this when the transferring context size in the build output runs to hundreds of MB. .git changes with every commit and breaks the COPY . . cache, and the host's node_modules can leak into the image.

# .dockerignore
.git
node_modules
dist
*.log
.env

Cause 3: where the ARG sits

When an ARG's value differs from the previous build, every RUN below that ARG declaration misses the cache. Declare values that change every build, such as ARG GIT_SHA, after the dependency install.

Cause 4: CI runners have no local cache

A GitHub-hosted runner is a fresh VM for every job, so the cache has to be stored outside and loaded back.

- uses: docker/setup-buildx-action@v3
- uses: docker/build-push-action@v6
  with:
    push: true
    tags: registry.example.com/checkout:${{ github.sha }}
    cache-from: type=gha
    cache-to: type=gha,mode=max

To keep the cache in a registry, use --cache-from type=registry,ref=registry.example.com/checkout:buildcache and --cache-to type=registry,ref=registry.example.com/checkout:buildcache,mode=max. The default docker driver cannot export cache and fails with cache export feature is currently not supported for docker driver, so create a builder with setup-buildx-action. A cache mount (RUN --mount=type=cache,target=/root/.npm npm ci) cuts package downloads further, but cache mount contents are not exported by cache-to.

What not to do

  • Adding --no-cache to CI permanently because a build looked odd — the symptom goes away, but every build takes the slowest path.
  • Copying the host's node_modules to skip the install — native modules built for another OS or architecture get mixed in.

The order to work in

  1. Run with --progress=plain and find the first step that is not CACHED
  2. Reorder to copy the lockfile → install → copy the source
  3. Put .git and node_modules in .dockerignore and move changing ARGs below the install
  4. In CI, wire up a type=gha or registry cache

Check the same cause in a one-minute quiz: Docker build cache not used: npm ci reruns even when only the README changed.

Quick diagnostic checklist

  • Run docker build --progress=plain and find the first step that is not CACHED.
  • Check whether COPY . . sits above npm ci or pip install.
  • Copy only package.json and package-lock.json, install, then copy the source.
  • If the transferring context size is large, add .git and node_modules to .dockerignore.
  • Declare an ARG that changes every build after the dependency install.
  • If it is CACHED locally but reruns in CI, the CI runner simply has no cache.
  • Set cache-from: type=gha and cache-to: type=gha,mode=max on build-push-action.
  • Do not hide the symptom by adding --no-cache permanently.

Practice with this guide

Reviewed problems about the same failure. Write the cause, recovery, and prevention yourself, then compare with the model answer.

Read next