Sub topic 12
Fixing permission and ownership problems
This chapter assumes you can run containers with -v or --mount, read ls -l output, and know that a container process runs as a user such as root. It covers why mounted data produces "Permission denied" errors and how to fix them.
How Linux decides who can write
The kernel checks numeric user IDs (UIDs) and group IDs (GIDs). It never checks names. A file owned by UID 1000 on the host belongs to UID 1000 in the container, even if the container's /etc/passwd calls that user something else or has no such user.
Two rules follow from this:
- A bind mount shows the host directory's real owner and mode. Docker changes nothing.
- A new named volume copies ownership and mode from the image's directory at the mount path. If the image has no content at that path, the volume starts owned by
root.
By default, most containers run as root (UID 0). Root can write almost anywhere, so problems usually show up when an image drops privileges. The PostgreSQL image does this, for example, and many images set USER in their Dockerfile.
Reproduce the problem
On Linux, a root container writing into a bind mount leaves root-owned files on your host:
mkdir -p out
docker run --rm -v "$(pwd)/out":/out alpine touch /out/a.txt
ls -ln outThe -n flag prints numeric IDs. The file a.txt is owned by 0 0, so your normal user may not be able to edit or delete it.
The reverse also happens. A container running as UID 999 cannot write to a host directory owned by UID 1000 with mode 755.
Diagnose
flowchart TDUse the diagram below as a checklist.
Run these two commands to get the facts:
docker run --rm alpine id
docker run --rm -v pgdata:/data alpine ls -lnd /dataThe first shows the user the image runs as. To check a specific image, add --entrypoint id before the image name. The second shows the numeric owner and mode of the mounted path.
Fixes
Pick the fix that matches the cause.
- Run as your host user. This suits bind mounts in development.
``bash docker run --rm --user "$(id -u):$(id -g)" \ -v "$(pwd)/out":/out alpine touch /out/b.txt ``
- Change ownership of a volume. Use a short-lived root container, and use the UID the application runs as:
``bash docker run --rm -v appdata:/data alpine chown -R 1000:1000 /data ``
- Fix it in the image. Create the directory and
chownit in the Dockerfile before the volume is first used. The new volume then inherits the correct owner.
``dockerfile FROM alpine:3.20 RUN addgroup -g 1000 app && adduser -D -u 1000 -G app app \ && mkdir /data && chown app:app /data USER app VOLUME /data ``
- Adjust the mode on the host. Use
chmodorchownon the host directory. Avoidchmod 777outside throwaway tests.
Other causes
- Read-only mounts. A mount with
:roorreadonlyrejects every write, even from root. The error message is "Read-only file system", not "Permission denied". - SELinux. On Fedora, RHEL, and similar systems, a bind mount can fail even when ownership is correct. Add
:z(shared among containers) or:Z(private to one container) to the-voption. Never relabel system directories such as/homeor/etc. - Docker Desktop and rootless Docker. File sharing or user namespaces remap IDs, so what you see on the host can differ from what the container sees. Check the numeric IDs from inside the container.
Exercise
A container whose image sets USER 999 fails with "Permission denied" when it writes to a bind-mounted host directory owned by UID 1000 with mode 755. Name two ways to fix it without making the directory world-writable.
Show answer
Run the container as the directory's owner with --user 1000:1000. Or change the host directory's owner to UID 999 with chown 999 <dir>. Either way the container's numeric UID matches the directory owner.
Exercise
A new named volume is mounted at a path that does not exist in the image. Who owns the volume's root directory, and how can you change it?
Show answer
It is owned by root. Run a temporary container as root that mounts the volume and runs chown -R UID:GID /data. Or create the directory with the right owner in the Dockerfile.