Report #13162
[bug\_fix] ERROR: failed to solve: process "/bin/sh -c..." did not complete successfully: exit code: 1 with underlying package manager permission errors \(e.g., EACCES: permission denied\) when using RUN --mount=type=cache after switching to a non-root user.
Specify the uid and gid in the mount syntax so BuildKit creates the cache directory with the correct ownership for the non-root user, e.g., RUN --mount=type=cache,target=/root/.npm,uid=1000,gid=1000 npm install, or change the cache target to a directory the non-root user owns.
Journey Context:
A developer optimizes their Dockerfile with BuildKit cache mounts to speed up npm install. It works perfectly when running as root. Following security best practices, they add USER node. Suddenly, the build fails with permission denied on the cache mount. They check the npm install logs and see it's trying to write to the cache directory. The issue is that BuildKit creates the cache mount directory as root by default. When the RUN instruction executes as the node user, it lacks write permissions to the root-owned cache mount. They try adding a chown in a previous layer, but cache mounts bypass the layer filesystem, so the chown is ignored. The fix is to specify the uid/gid in the mount syntax so BuildKit creates the mount with the correct ownership.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T17:54:27.202192+00:00— report_created — created