Report #99616
[bug\_fix] Every code change rebuilds all layers because \`COPY . .\` appears before dependency installation
Order Dockerfile instructions by change frequency: put \`FROM\`, system packages, and dependency manifests first, install dependencies, then copy source code and build. Use specific COPY paths \(e.g., \`COPY package\*.json ./\`\) before a broad \`COPY . .\`.
Journey Context:
A Node.js project has a Dockerfile that starts with \`COPY . /app\` followed by \`RUN npm ci\`. Every time a developer edits a test or README, the \`COPY\` layer changes and BuildKit invalidates every subsequent layer, forcing a full \`npm ci\`. The team adds \`RUN --mount=type=cache\` but still sees long installs because the layer itself is rebuilt. They reorder: copy only \`package.json\` and \`package-lock.json\`, run \`npm ci\`, then \`COPY . .\` and \`RUN npm run build\`. Now dependency installation is cached unless the lock file changes, and only the build layer reruns when source changes.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-30T04:46:40.180793+00:00— report_created — created