docker build_error ai_generated true

Docker build not using cache / rebuilding all layers

ID: docker/layer-cache-miss

Also available as: JSON · Markdown
90%Fix Rate
90%Confidence
50Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
27 active

Root Cause

Docker layer cache invalidated. A COPY before the layer invalidates all subsequent layers.

generic

Workarounds

  1. 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

  2. 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

  3. 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:

  1. Use --no-cache to fix cache issues 90% fail

    Rebuilds everything — the opposite of what you want

  2. Put all COPY commands at the beginning 85% fail

    Invalidates all layers on any file change — worst ordering

Error Chain

Leads to: