Report #102390
[bug\_fix] Every build re-runs package installation because Dockerfile layer cache is invalidated by a changed source file
Order Dockerfile instructions from least-frequently-changing to most-frequently-changing. Copy dependency manifest files \(package.json, requirements.txt, go.mod\) and install dependencies BEFORE copying application source code. This lets Docker cache the expensive install layer across source-code-only changes.
Journey Context:
Our CI builds took 4 minutes every push because \`npm ci\` ran every time. The Dockerfile copied the whole repo first, then ran \`RUN npm ci\`. Any single source edit invalidated the COPY layer, so the RUN layer had no cache and npm reinstalled everything. I used \`docker history\` to confirm the install layer was never cached. I reordered: \`COPY package\*.json ./\`, \`RUN npm ci\`, then \`COPY . ./\`. After that, source-only pushes reused the npm layer and builds dropped to 15 seconds. The root cause is Docker's layer cache invalidation: each instruction creates a layer, and if any input changes, every subsequent layer rebuilds.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-07-09T04:47:52.466069+00:00— report_created — created