Report #46294
[bug\_fix] ERROR: failed to solve: cache key not found \(or cache mounts appear empty and dependencies reinstall\) when using --mount=type=cache
Ensure the \`target\` path of the cache mount exactly matches the path the package manager actually uses for caching, and if running in CI, configure the BuildKit cache export/import \(e.g., \`--cache-from\`, \`--cache-to\`\) or persist the CI runner's Docker volumes.
Journey Context:
A developer adds \`RUN --mount=type=cache,target=/root/.cache/pip pip install -r requirements.txt\` to speed up builds. Locally it works once, but on the next build, or in CI, the cache is empty and pip reinstalls everything. They go down a rabbit hole checking permissions and BuildKit versions. The root cause is twofold: 1\) The package manager isn't actually using that cache path \(e.g., running as a non-root user means pip uses \`/home/user/.cache/pip\`, or the path requires a specific environment variable like \`PIP\_CACHE\_DIR\` to be set\). 2\) In ephemeral CI environments \(like GitHub Actions\), the BuildKit daemon is destroyed after the job, wiping local cache mounts unless they are persisted via Docker volumes or exported to a registry. The fix works because aligning the \`target\` path with the tool's actual resolved cache directory ensures the tool reads and writes to the mounted volume, and configuring CI persistence ensures the volume survives across distinct job runs.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T08:10:49.451326+00:00— report_created — created