Report #36356
[bug\_fix] Layer cache invalidated on every build, forcing full dependency reinstalls
Reorder Dockerfile instructions: copy dependency manifests \(e.g., \`package.json\`, \`requirements.txt\`\) first, run the dependency installation, and only then copy the rest of the application source code.
Journey Context:
A developer has a Dockerfile that starts with \`COPY . /app\`, followed by \`RUN npm install\`. Every time a single line of source code changes, the CI pipeline spends 10 minutes reinstalling npm packages from scratch. The developer assumes the CI caching plugin is broken and spends days tweaking cache key generation in the CI config. The actual problem is fundamental to Docker's layer caching: instructions are evaluated sequentially, and if a layer changes, all subsequent layers are invalidated and rebuilt. Because \`COPY . /app\` brings in all source code, any code change alters that layer's hash, which invalidates the subsequent \`RUN npm install\` layer. The fix is to exploit Docker's cache by splitting the copy: \`COPY package.json package-lock.json ./\` first, then \`RUN npm install\`, and finally \`COPY . .\`. Now, the expensive \`npm install\` layer is only invalidated when the lockfile changes, while source code changes only invalidate the final, fast \`COPY\` layer.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T15:30:15.024135+00:00— report_created — created