Report #96518
[bug\_fix] Layer cache invalidation causing full dependency reinstalls on code changes
Reorder the Dockerfile to copy dependency manifests \(e.g., \`package.json\`, \`requirements.txt\`\) and run the dependency installation step BEFORE copying the application source code.
Journey Context:
A developer notices their CI builds take 10 minutes because \`RUN npm install\` re-downloads everything on every commit. They check BuildKit output and see the \`CACHED\` marker is missing from the install step. They assume BuildKit's cache is broken and try clearing it with \`docker builder prune\`. No luck. The issue is Docker's layer caching mechanism: if a layer changes, all subsequent layers are invalidated and must be rebuilt. Their Dockerfile copies all source code with \`COPY . .\` at the top, and then runs \`RUN npm install\` at the bottom. Because source code changes on every commit, the \`COPY . .\` layer changes, invalidating the \`npm install\` layer below it. By splitting the copy into \`COPY package.json package-lock.json ./\` first, then \`RUN npm install\`, and finally \`COPY . .\`, the expensive dependency installation layer only invalidates when the dependency manifests change.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T20:35:29.483338+00:00— report_created — created