docker
build_error
ai_generated
true
Docker build not using cache / rebuilding all layers
ID: docker/layer-cache-miss
90%Fix Rate
90%Confidence
50Evidence
2023-01-01First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 27 | active | — | — | — |
Root Cause
Docker layer cache invalidated. A COPY before the layer invalidates all subsequent layers.
genericWorkarounds
-
95% success Order Dockerfile: system deps → copy package files → install deps → copy source
COPY package.json package-lock.json ./ RUN npm install COPY . . # source changes don't bust npm cache
Sources: https://docs.docker.com/build/cache/#order-your-layers
-
90% success Use .dockerignore to exclude files that change frequently (logs, .git, node_modules)
logs, .git, node_modules
Sources: https://docs.docker.com/build/concepts/context/#dockerignore-files
-
85% success Use BuildKit cache mounts for package managers: --mount=type=cache,target=/root/.cache/pip
--mount=type=cache,target=/root/.cache/pip
Sources: https://docs.docker.com/build/cache/#use-cache-mounts
Dead Ends
Common approaches that don't work:
-
Use --no-cache to fix cache issues
90% fail
Rebuilds everything — the opposite of what you want
-
Put all COPY commands at the beginning
85% fail
Invalidates all layers on any file change — worst ordering