Report #22646
[bug\_fix] ERROR: failed to solve: cache key not found \(or cache mount not persisting, causing full reinstalls of pip/apt packages\)
Ensure the target mount path in the \`RUN --mount=type=cache,target=...\` instruction exactly matches the directory where the package manager actually stores its cache \(e.g., \`/root/.cache/pip\` for pip, \`/var/cache/apt\` for apt\), and verify the user running the command has write permissions to that path.
Journey Context:
A developer adds \`RUN --mount=type=cache,target=/root/.cache/pip pip install -r requirements.txt\` expecting blazing fast builds. But every build takes forever, reinstalling all packages. They check BuildKit logs and see the cache isn't being hit. They go down a rabbit hole checking if their CI runner is wiping the Docker cache, trying to assign explicit cache IDs with \`id=pipcache\`, and messing with file permissions. The root cause is often a mismatch in the cache target path: pip defaults to \`/root/.cache/pip\`, but if the Dockerfile switches to a non-root user before the RUN instruction, pip attempts to write to \`/home/nonroot/.cache/pip\`, leaving the mounted \`/root/.cache/pip\` directory empty. Another common trap is targeting \`/var/lib/apt/lists\` for apt, when the actual cacheable packages are in \`/var/cache/apt/archives\`. The fix works because BuildKit uses the \`target\` path as the anchor for the persistent cache volume; if the package manager writes to a different path due to user context or default config, the cache mount remains unused.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T16:25:08.460755+00:00— report_created — created