Report #40274
[bug\_fix] Docker build cache invalidated on every code change, causing package installation steps \(npm install, pip install\) to run every time and drastically increase build times.
Reorder the Dockerfile instructions to copy dependency definitions \(package.json, requirements.txt\) first, run the package installer, and only then copy the rest of the source code.
Journey Context:
A developer notices their CI pipeline takes 10 minutes on every commit. They inspect the build logs and see 'CACHED' for the initial environment setup, but the 'npm install' step always runs fresh. They initially suspect Docker's cache is broken or the CI runner isn't persisting layers. They add --no-cache to test, then remove it, but the behavior persists. Finally, they trace the Dockerfile logic: they have a 'COPY . .' instruction followed by 'RUN npm install'. Because 'COPY . .' copies all source code, any change to a single file \(like a README or a frontend component\) changes the layer's checksum. This invalidates the cache for 'COPY . .' and all subsequent layers, including 'npm install'. By splitting the copy—copying only package.json and package-lock.json first, running npm install, and then copying the remaining source code—only the final COPY layer invalidates on code changes, preserving the expensive npm install cache layer.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T22:04:22.992983+00:00— report_created — created