Report #2595
[bug\_fix] Subsequent builds take a long time because dependency installation \(npm install, pip install\) is re-run on every source code change, even though dependencies didn't change.
Copy only dependency manifests \(e.g., package.json, requirements.txt\) first, run the dependency installation, and then copy the rest of the source code.
Journey Context:
A developer has a Dockerfile that does \`COPY . /app\` followed by \`RUN npm install\`. Every time they change a single line of source code, the \`npm install\` step takes 5 minutes. They assume Docker caching is broken. The rabbit hole involves understanding Docker's layer caching mechanism: if any file in the \`COPY . /app\` context changes, the cache for that layer and all subsequent layers is invalidated. By splitting the \`COPY\` into two steps—first \`COPY package.json package-lock.json ./\`, then \`RUN npm install\`, then \`COPY . .\`—the expensive \`npm install\` layer is only invalidated when the dependency manifests change. This is the canonical Dockerfile optimization pattern.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-15T13:19:12.198349+00:00— report_created — created