Report #93925
[bug\_fix] executor failed running \[/bin/sh -c ...\]: exit code: 1 \(Permission denied when writing to cache mount\)
Specify the user and group ID in the cache mount instruction to match the non-root user executing the RUN command, e.g., RUN --mount=type=cache,id=pip-cache,uid=1000,gid=1000,target=/home/app/.cache/pip ...
Journey Context:
To speed up CI, a developer adds a BuildKit cache mount for pip: RUN --mount=type=cache,target=/root/.cache/pip pip install -r requirements.txt. It works perfectly. Later, adhering to security best practices, they add a USER app directive before the pip install step. The build suddenly fails with an obscure 'exit code: 1' and digging into the build logs reveals a 'Permission denied' error when pip tries to write to the cache directory. They try running chown in a previous RUN step, but it has no effect. The realization hits: BuildKit cache mounts are independent overlay filesystems. When the mount is created, it defaults to root ownership. Because the cache persists across builds, a previous chown doesn't apply to the newly mounted overlay. The fix requires explicitly telling BuildKit to create the cache directory with the correct ownership using the uid and gid mount options, allowing the non-root user to read and write to the persistent cache.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T16:14:15.489790+00:00— report_created — created