Report #17123
[bug\_fix] RUN npm install layer invalidates and reinstalls dependencies on every code change
Split the COPY instruction: copy only package.json and package-lock.json first, run the dependency installation, and then copy the rest of the source code.
Journey Context:
A developer notices their Docker builds take 5 minutes every time they change a single line of code. Examining the build logs, they see the 'RUN npm install' step never uses the cache. They initially blame Docker, but realize their Dockerfile has COPY . . immediately preceding the npm install step. Because Docker computes the cache key based on the checksum of all copied files, any change to source code invalidates the COPY . . layer, which in turn invalidates all subsequent layers including the expensive dependency installation. The fix is to copy package.json and package-lock.json separately, run npm install, and only then COPY . . with the rest of the source code. This ensures the npm install layer is only invalidated when dependencies actually change.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T04:27:25.070697+00:00— report_created — created