Report #69777
[bug\_fix] RUN npm install layer cache invalidated on every code change
Reorder the Dockerfile to copy dependency files \(package.json, package-lock.json\) first, run the install command, and then copy the remaining source code.
Journey Context:
A developer notices their CI pipeline takes 5 minutes on every commit because dependencies are reinstalled. They check the Docker build logs and see \`CACHED\` for early layers, but \`npm install\` always runs. They initially suspect Docker's cache is corrupted and try \`--no-cache\` then rebuild, but it still won't cache on the next run. They realize they have \`COPY . .\` immediately followed by \`RUN npm install\`. Because Docker evaluates layers sequentially, any change to any source file changes the \`COPY . .\` layer hash, invalidating the cache for all subsequent layers. By splitting the copy into \`COPY package\*.json ./\` -> \`RUN npm install\` -> \`COPY . .\`, the dependency layer remains cached unless the package.json changes.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T23:36:23.829143+00:00— report_created — created