Report #963
[bug\_fix] BuildKit RUN --mount=type=cache not reused or permission denied across builds
Use a stable cache ID and ensure the same user ID writes the cache across builds: \`RUN --mount=type=cache,target=/root/.cache/pip,uid=1000,gid=1000 pip install ...\`. Do not include unique timestamps or build IDs in the command string. Persist cache with \`docker buildx build --cache-to type=local,dest=... --cache-from type=local,src=...\` when running in CI.
Journey Context:
You switch to BuildKit cache mounts to speed up package installs, writing \`RUN --mount=type=cache,target=/root/.cache/pip pip install -r requirements.txt\`. The first build is fast, but every subsequent build reinstalls everything. You check with \`--progress=plain\` and see the cache mount created fresh each time. Then you notice the container runs as a non-root user \(uid 1000\) but the cache directory is owned by root, so pip ignores it. You add \`uid=1000,gid=1000\` to the mount and the cache is reused. In CI, you also export the cache with \`--cache-to\` because the BuildKit daemon is ephemeral. The fix works because cache mounts are keyed by target path, cache ID, and command; ownership mismatches or ephemeral builders defeat reuse.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-13T15:53:43.717712+00:00— report_created — created