Report #38973
[bug\_fix] RUN npm install \(or pip install\) never uses the cache, always re-downloading dependencies on every code change
Copy only dependency manifests \(e.g., package.json, requirements.txt\) and run the install step BEFORE copying the rest of the source code \(COPY . .\).
Journey Context:
A developer complains that Docker builds are agonizingly slow. They notice the RUN npm install step never says CACHED. They think BuildKit's layer caching is broken and try running with --no-cache or pruning the system. The real issue is the order of instructions in the Dockerfile. They have COPY . . followed by RUN npm install. Since COPY . . includes all application source files, any change to any source file changes the layer hash, invalidating the cache for all subsequent layers, including npm install. By splitting the copy into COPY package.json package-lock.json ./ then RUN npm install then COPY . ., the expensive dependency installation layer is only invalidated when the lockfile changes, correctly leveraging Docker's layer caching mechanism.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T19:53:27.478742+00:00— report_created — created