File ownership and permissions in mounts
Why containers hit Permission denied on mounted files: the kernel compares numeric UIDs, not names. Matching UIDs, --user, chown-then-drop entrypoints.
File ownership and permissions in mounts
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Permission checks on mounted files compare numeric UIDs and GIDs. User names are just labels in each side's own /etc/passwd and do not cross the mount.
- Without user namespaces, UID 0 in the container is UID 0 on the host, so files a root container writes into a bind mount are owned by root on the host.
- A non-root container user can only write to a mounted directory if its UID or GID has permission there, which usually means matching the host user's UID.
- Fixes, from simplest: run with --user "$(id -u):$(id -g)", build the image with the user's UID as a build argument, or start as root, chown, then drop privileges with gosu.
- A fresh named volume copies the ownership of the image's directory, so create data directories with the right owner in the Dockerfile.
- Rootless Docker and userns-remap map container UIDs to an unprivileged range on the host, which changes what owners look like from outside.
Permission denied, but I’m the owner
A container that writes to a bind-mounted directory fails with Permission denied. You check on the host: the directory is yours, and the container’s user is called app, which seems fine. Or the reverse: a build container leaves a dist/ folder on your laptop that you cannot delete without sudo. Both come from one fact about how Linux checks permissions.
Numbers, not names
The kernel stores a numeric UID and GID on every file and compares them to the process’s numeric IDs. User names live in /etc/passwd, and the container has its own /etc/passwd from the image. So app in the container and alice on the host are only labels, and what matters is whether both are, say, UID 1000. The users and groups lesson covers the files.
Without a user namespace, a container shares the host’s ID space. UID 0 inside is UID 0 outside. That gives the two classic failures:
- Root container, bind mount. Files the container creates are owned by host root. Your editor and
rmcannot touch them. - Non-root container, bind mount. The container’s UID does not match the directory’s owner, so it cannot write.
Think of numbered lockers in a shared changing room. Two clubs use the room, each with its own member list. Your key opens locker 1000 whatever your name is on your own club’s list, and someone else with 1000 on their list opens the same locker.
Real image, real fix
The official postgres:18 image shows both the problem and a standard fix. Its first layer, which I pulled from Docker Hub, creates the user and its directories with fixed numbers:
groupadd -r postgres --gid=999
useradd -r -g postgres --uid=999 --home-dir=/var/lib/postgresql ...
drwxrwxrwt 0 999 999 0 Nov 24 09:58 var/lib/postgresql/
drwxrwsrwt 0 999 999 0 Nov 24 09:58 run/postgresql/
Mount a volume or directory owned by anything else at the data path and Postgres, as UID 999, could not write. So the entrypoint script starts as root and fixes ownership before dropping privileges. From the script in the image:
# allow the container to be started with `--user`
if [ "$user" = '0' ]; then
find "$PGDATA" \! -user postgres -exec chown postgres '{}' +
find /run/postgresql \! -user postgres -exec chown postgres '{}' +
fi
...
if [ "$(id -u)" = '0' ]; then
# then restart script as postgres user
exec gosu postgres "$BASH_SOURCE" "$@"
fi
Chown what is not already owned by postgres, then exec gosu postgres to re-run as UID 999. The database process never runs as root. If you start the container with --user, it skips the chown and assumes you set up ownership yourself.
Your options
Run as your own UID. For development tools that write into your project:
docker run --rm --user "$(id -u):$(id -g)" -v "$PWD":/src -w /src node:24-slim npm run build
Files come out owned by you. The process has no entry in the container’s /etc/passwd, which some tools complain about. HOME may also point somewhere unwritable.
Bake the UID in at build time. Nick Janetakis’s approach in the video:
ARG UID=1000
ARG GID=1000
RUN groupadd --gid "$GID" app && useradd --uid "$UID" --gid "$GID" --create-home app
USER app
docker build --build-arg UID="$(id -u)" --build-arg GID="$(id -g)" -t notes:dev .
Good for a development image that each developer builds. Not for a published image, which cannot know every host’s UIDs.
Chown, then drop. The postgres pattern: an entrypoint that starts as root, fixes ownership of its own data paths, and execs the server as the unprivileged user with gosu or setpriv. Keep the chown narrow. A recursive chown -R on a huge volume at every start is slow.
Let a named volume inherit. An empty named volume copies the image directory’s owner and mode. Create data directories in the Dockerfile with the owner you run as, RUN install -d -o app -g app /var/lib/notes or COPY --chown, and a new volume starts out writable. This is why named volumes often work where bind mounts fail.
Group access. Give a shared host directory a group, add that GID to the container with --group-add, and set the setgid bit so new files keep the group, as the special permission bits lesson shows.
When the numbers are remapped
Two setups change the picture by putting containers in a user namespace:
- Rootless Docker. The daemon runs as your user, and the container’s UID 0 maps to your UID on the host. Other container UIDs map into your range in
/etc/subuid, so a file written by container UID 999 appears on the host as something like UID 100998. Covered in running as non-root and rootless Docker. userns-remap. A daemon setting that does the same for a rootful daemon, mapping container root to an unprivileged host range.
Both make “root in the container” much less dangerous, and both mean host-side ownership of bind-mounted files looks different from what the container sees. Docker Desktop also translates ownership in its file sharing, so these errors show up most on native Linux.
SELinux adds a separate check on top of UIDs. If the numbers match and access is still denied on Fedora or RHEL, look at the :z and :Z labels in the bind mounts lesson.
About the video
The embed is Nick Janetakis’s “Running Docker Containers as a Non-root User with a Custom UID / GID”, October 2022, 16.5 minutes. Its chapters cover why matching UIDs matters for volumes, demonstrating the problem with a UID other than 1000, setting build arguments for the UID and GID and assigning them to a user and group, passing them in from docker-compose.yml and a .env file, and confirming the fix. It does not cover the chown-and-drop entrypoint or user namespaces, which the lesson adds. English was judged from the channel and the English caption track. The deeper video is a short 2026 explainer from a small channel.
What was run: the postgres:18 layers and entrypoint script were fetched from Docker Hub’s registry API with curl, the ownership listing is from tar -tzvf on its first layer, and the script excerpt is from the extracted file. The named-volume copy behaviour was checked in the moby and containerd continuity sources. The remapping behaviour is from the rootless and userns-remap pages, checked 2026-11-28. No container was run.
Where this fits
Data now lives in volumes with the right owners. Next, getting it out again: backing up and migrating volume data.
Check yourself
Your container runs as a user named app with UID 1000. On the host, your user is alice with UID 1001. The app cannot write to a bind-mounted ./data owned by alice. Why?
The kernel compares numbers. UID 1000 is not 1001 and the directory is not writable by others, so the write is denied. Names never enter into it.
After running a build tool in a root container with ./ bind-mounted, you cannot delete files in ./dist on the host without sudo. Why, and how do you avoid it?
The container ran as UID 0, which is root on the host too, so the files it created are owned by root. Run the container with --user "$(id -u):$(id -g)" so files are created with your UID.
How does the official postgres image cope with a data volume owned by the wrong user?
Its entrypoint starts as root, runs find "$PGDATA" ! -user postgres -exec chown postgres {} +, then re-executes itself as postgres with gosu. So ownership is fixed once and the server never runs as root.
Why does a named volume often just work for a non-root image while a bind mount does not?
An empty named volume is filled from the image, copying the directory's owner and mode, so it starts owned by the image's user. A bind mount keeps the host directory's owner.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.