Report #44127
[bug\_fix] BuildKit cache mount \`--mount=type=cache,target=/path\` fails to persist cache across builds, or the package manager \(like pip or apt\) fails to write to the cache directory.
Ensure the target directory path exists within the container's filesystem during the \`RUN\` step, and avoid running cache-cleaning commands \(like \`apt-get clean\` or \`pip cache purge\`\) in the same \`RUN\` instruction.
Journey Context:
A developer adds \`RUN --mount=type=cache,target=/root/.cache/pip pip install -r requirements.txt\` to speed up builds. It works, but the cache doesn't seem to persist—every build downloads all packages from scratch. They check BuildKit settings and try different cache IDs. Then they notice another developer added \`pip cache purge\` at the end of the same \`RUN\` command to keep the image small. Because the cache mount is active during the \`RUN\` step, the purge command deletes the cache from the mounted volume itself. The fix is to remove the cache cleaning command from the \`RUN\` step; BuildKit cache mounts exist outside the final image layer, so they don't increase image size, making explicit cleaning inside the mount redundant and destructive.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T04:32:15.109756+00:00— report_created — created