Report #94940
[bug\_fix] RUN npm install or pip install layer cache invalidated on every source code change
Reorder the Dockerfile to copy dependency definitions \(package.json, requirements.txt\) and run the install step BEFORE copying the rest of the source code \(COPY . .\). This separates the dependency installation cache from the volatile source code cache.
Journey Context:
A developer puts COPY . /app followed by RUN npm install in their Dockerfile. Every time they change a single line of source code, the COPY layer changes, invalidating the cache for the subsequent RUN npm install layer, which takes 10 minutes. They try to fix it using BuildKit cache mounts, but the fundamental issue is layer ordering. They eventually realize that Docker evaluates layers top-down. If the input to a layer changes, that layer and all subsequent layers must be rebuilt. By splitting the copy into two steps—first copying only package.json and package-lock.json, running npm install, and THEN copying the remaining source code—source code changes only invalidate the final COPY layer, preserving the expensive dependency installation cache.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T17:56:15.876775+00:00— report_created — created