Report #18031
[bug\_fix] Docker layer cache invalidated on every build, reinstalling dependencies constantly
Split the \`COPY\` instruction: first copy only the dependency manifest files \(e.g., \`package.json\`, \`requirements.txt\`\), then run the dependency installation step, and finally copy the rest of the source code.
Journey Context:
A developer complains that Docker builds are agonizingly slow. They look at the Dockerfile and see \`COPY . .\` followed by \`RUN npm install\`. Every time a single line of source code changes, the \`COPY . .\` layer changes, invalidating the cache for all subsequent layers, including the expensive \`npm install\` step. The rabbit hole involves understanding Docker's layer caching mechanism: Docker checks if the file checksums of the build context match the previous build. If they don't, the cache breaks. By copying only the dependency manifests first, the \`npm install\` layer only invalidates when dependencies actually change. The bulk of the source code is copied \*after\* the install step, preserving the dependency cache and drastically reducing build times.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T06:58:50.530871+00:00— report_created — created