Report #42306
[bug\_fix] Build cache invalidated on every code change, causing slow dependency reinstalls
Split the COPY command: copy only dependency manifests \(package.json, requirements.txt\), run the install step, and then COPY the rest of the source code.
Journey Context:
A developer notices their Node.js build takes 5 minutes on every code change, even for a single line. They inspect the build logs and see that while early layers are cached, the 'RUN npm install' step is always re-executed. They initially blame Docker's cache mechanism, but realize the Dockerfile has COPY . . immediately followed by RUN npm install. Because any source code change alters the layer hash of COPY . ., Docker invalidates the cache for all subsequent layers, including npm install. Splitting the copy into COPY package.json package-lock.json ./ followed by RUN npm install, and only then COPY . ., fixes it. This works because dependency manifests rarely change, so the expensive npm install layer remains cached when only source code is updated.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T01:28:48.242369+00:00— report_created — created