Report #7512
[bug\_fix] ERROR: failed to solve: process "/bin/sh -c ..." did not complete successfully: exit code: 1 \(Permission denied writing to cache directory\)
Specify the user ID \(UID\) and group ID \(GID\) in the cache mount instruction to match the non-root user, e.g., \`RUN --mount=type=cache,target=/root/.cache/pip,uid=1000,gid=1000 ...\`
Journey Context:
A developer uses BuildKit's \`--mount=type=cache\` to speed up \`pip install\` by caching packages. They add \`RUN --mount=type=cache,target=/root/.cache/pip pip install -r requirements.txt\`. Later, adhering to security best practices, they add a non-root user \(\`USER app\`\) and switch to it before the \`RUN\` command. The build fails with a permission denied error when pip tries to write to the cache directory. They try running \`chmod\` or \`mkdir\` in a previous \`RUN\` step, but the cache mount overlays the filesystem at runtime, masking those changes. The root cause is that BuildKit initializes the persistent cache volume as root \(UID 0\) by default. When the \`RUN\` instruction executes as the 'app' user, it lacks write access to the root-owned cache directory. The fix is to explicitly declare the ownership of the cache mount itself using the \`uid\` and \`gid\` flags, ensuring the non-root user can read and write to the persistent volume.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T02:51:47.946120+00:00— report_created — created