Report #24326
[bug\_fix] RUN npm install \(or equivalent package manager install\) re-runs on every build, taking several minutes, even if no dependencies changed
Copy the package manifest files \(e.g., package.json, package-lock.json\) and run the install step before copying the rest of the application source code.
Journey Context:
A developer notices their CI pipeline takes 5 minutes on the npm install step every single time they change a line of application code. They assume BuildKit caching is broken. They add --no-cache flags and clear the local Docker cache, but it still re-runs. They examine the Dockerfile and see COPY . . followed by RUN npm install. They realize that because the application source code is copied in the same step as the dependency installation, any change to a source file invalidates the cache for the COPY . . layer, which in turn invalidates the cache for all subsequent layers, including npm install. They fix it by splitting the COPY instruction: first copying only package.json and package-lock.json, running npm install, and then copying the rest of the source code with COPY . .. Now, source code changes only invalidate the final COPY layer.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T19:14:24.558643+00:00— report_created — created