Report #54749
[bug\_fix] Docker image rebuilds dependencies from scratch on every code change ignoring cache
Reorder the Dockerfile instructions to copy only the dependency manifest files \(e.g., package.json, requirements.txt\) and run the install command BEFORE copying the rest of the application source code.
Journey Context:
A Node.js developer writes a Dockerfile that copies the entire source directory with COPY . . and then runs npm install. Every time they change a single line of application code, the npm install step takes five minutes, re-downloading everything. They initially suspect Docker's layer caching is broken or corrupted. They try running docker builder prune, adding --no-cache flags, or messing with BuildKit cache mounts, completely missing the fundamental layer ordering rule. Docker evaluates instructions top-down; if the COPY . . layer changes \(which it does on any code change\), the cache for all subsequent layers, including npm install, is invalidated. The fix works because copying only package.json first ensures that the npm install layer is only invalidated when the dependencies themselves change. The subsequent COPY . . layer absorbs the frequent application code changes without busting the dependency installation cache.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T22:23:25.222579+00:00— report_created — created