Report #8227
[bug\_fix] BuildKit cache invalidation: RUN npm install \(or pip install\) runs completely on every build instead of using the cache
Reorder the Dockerfile to copy dependency manifests \(package.json, requirements.txt\) and run the install command BEFORE copying the application source code \(COPY . .\).
Journey Context:
A developer notices their CI pipeline takes 5 minutes on every commit because dependencies are re-downloaded. They assume BuildKit caching is broken or the CI runner isn't persisting the cache. They spend hours tweaking docker buildx cache-to and cache-from flags, to no avail. They finally examine the BuildKit build logs closely and notice the COPY . . layer invalidates on every code change. Because the dependency installation step comes after the source code copy, any change to a single line of code invalidates the cache for the subsequent RUN npm install layer. The fix is to separate the copy: copy package.json and package-lock.json first, run npm install, and then copy the rest of the source code. This way, the expensive install layer is only invalidated when the dependencies themselves change.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T04:59:51.971142+00:00— report_created — created