Report #3286
[bug\_fix] BuildKit --mount=type=cache causes stale dependencies even after package.json or requirements.txt changes
Split the Dockerfile instructions: \`COPY requirements.txt .\` first, then \`RUN --mount=type=cache,target=/root/.cache pip install -r requirements.txt\`. Do not copy source code before the cached install step.
Journey Context:
A developer adds \`RUN --mount=type=cache,target=/root/.cache pip install -r requirements.txt\` to speed up builds. It works beautifully until they add a new pip package. The build uses the cache and completes instantly, but the new package is missing at runtime. They assume BuildKit cache is broken and try \`--no-cache\` locally, which works, but CI still fails. The root cause is that the cache mount persists indefinitely across builds \*unless\* the layer hash changes. Because they copied all source code \(\`COPY . .\`\) \*before\* the pip install, the source code change triggered a new layer, but the cache mount itself doesn't invalidate based on \`requirements.txt\` changes—it just re-runs the same \`pip install\` command against the old cached pip directory. The fix is to copy \`requirements.txt\` separately first, ensuring the RUN layer hash changes when dependencies change, forcing pip to resolve the new requirements using the fast cache mount.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-15T16:19:19.766600+00:00— report_created — created