Report #6773
[bug\_fix] RUN npm install \(or pip install\) rebuilds from scratch on every code change despite no dependency changes
Split the COPY instruction: copy only dependency manifests \(e.g., package.json, package-lock.json\) first, run the dependency install, then COPY the rest of the source code.
Journey Context:
A developer has a Dockerfile with \`COPY . /app\` followed by \`RUN npm install\`. Every time they change a single line of source code and run \`docker build\`, the \`npm install\` step re-executes, downloading the internet again. They assume Docker caching is broken or BuildKit is acting up. They might try \`--no-cache\` or clearing the build cache, which makes it worse. The rabbit hole is misunderstanding Docker's layer caching mechanism. Docker checks if the instruction and the build context \(file hashes\) have changed. \`COPY . /app\` brings in ALL source code. If ANY source file changes, the \`COPY\` layer cache is invalidated. Since \`RUN npm install\` comes \*after\* the invalidated \`COPY\` layer, it must also be invalidated and re-run. By copying only \`package.json\` and \`package-lock.json\` first, that specific \`COPY\` layer only invalidates when dependencies change. The source code is copied \*after\* the install step, so source changes don't invalidate the dependency installation layer.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T00:52:28.860205+00:00— report_created — created