Report #103705
[bug\_fix] Layer cache invalidated unexpectedly after changing an unrelated file in the build context
Use .dockerignore to exclude files that are not needed for the build, and order COPY instructions from least to most frequently changed files.
Journey Context:
A developer had a Dockerfile that started with COPY . . then RUN npm install. Every time they changed a README.md file in the project root, the entire npm install layer was re-executed, adding minutes to the build. They suspected cache invalidation but didn't know why. The rabbit hole: Docker's layer caching uses checksums of the build context for COPY instructions. Since COPY . . copies the entire context, any change to any file \(even a comment in a doc\) changes the checksum, invalidating all subsequent layers. The fix: create a .dockerignore file to exclude README.md, .git, and other non-essential files. Then restructure the Dockerfile to copy package.json and package-lock.json first, run npm install, then copy the rest of the source. This leverages cache for the dependency installation layer as long as those two files haven't changed.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-07-12T20:06:20.778650+00:00— report_created — created