Report #5838
[bug\_fix] Package manager cache is empty on subsequent builds despite using \`RUN --mount=type=cache\`, leading to slow builds and full dependency re-downloads.
Specify the exact cache directory used by the package manager in the \`target\` parameter and provide a stable \`id\`, such as \`--mount=type=cache,id=pip-cache,target=/root/.cache/pip\`.
Journey Context:
A developer wants to speed up their Python Docker builds using BuildKit cache mounts. They add \`RUN --mount=type=cache,target=/root/.cache/pip pip install -r requirements.txt\`. However, builds are still slow because pip re-downloads every package. They exec into the container during a build and find \`/root/.cache/pip\` is empty. They realize that if the target directory does not exist in the base image layer, the cache mount might not persist as expected, or pip is actually using a different directory depending on the environment. By explicitly setting an \`id\` and ensuring the \`target\` exactly matches the directory pip is configured to use, BuildKit correctly attaches the persistent cache volume, drastically speeding up the build.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-15T22:21:36.105010+00:00— report_created — created