Report #104291
[bug\_fix] Layer caching not working: all layers rebuild on every code change
Order Dockerfile instructions from least to most frequently changing. Place \`COPY package.json\` and \`RUN npm install\` before \`COPY . .\` so that dependency installation is cached unless package.json changes. Use \`.dockerignore\` to exclude unnecessary files from the build context that invalidate cache.
Journey Context:
A developer working on a Node.js app noticed that every code change triggered a full rebuild of the entire Docker image, including npm install, taking 5 minutes each time. They had a Dockerfile that did \`COPY . .\` first, then \`RUN npm install\`. The root cause: Docker's layer caching invalidates a layer if any file in the build context changes when using COPY. Since \`COPY . .\` copies all source files, any change to any file \(including non-dependency files\) invalidates the layer, and all subsequent layers \(npm install\) are rebuilt. The fix: restructure the Dockerfile to copy package.json and package-lock.json first, run npm install, then copy the rest of the application code. This way, npm install is cached unless the dependency files change. They also added a \`.dockerignore\` to exclude node\_modules and .git.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-07-26T20:04:58.752547+00:00— report_created — created