Report #8605
[bug\_fix] Docker build cache invalidates unexpectedly on every build
Reorder the Dockerfile instructions to copy dependency files \(package.json, requirements.txt\) and run the install step BEFORE copying the rest of the application source code.
Journey Context:
A developer notices that npm install or pip install runs from scratch on every docker build, taking 10 minutes each time, even if no dependencies changed. They assume Docker caching is broken and try docker build --no-cache to 'reset' it, making it worse. They dive into BuildKit cache export features, completely overcomplicating the solution. The actual root cause is instruction ordering: they have COPY . . followed by RUN npm install. Docker evaluates the Dockerfile top-to-bottom. If ANY file in the source code changes \(even a README\), the COPY . . layer changes, invalidating the cache for all subsequent layers, including npm install. The fix works because copying package.json first creates a stable layer. As long as package.json doesn't change, Docker reuses the cached npm install layer, and only the final COPY . . layer is rebuilt with the new source code.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T05:52:57.860580+00:00— report_created — created