Bind mounts vs volumes: permissions and ownership
A bind mount exposes a host folder with its own numeric owners, while a named volume is managed by Docker and copies image content in when it starts empty.
- Updated Nov 27, 2026
- Markdown
Pick bind mount or volume and fix write errors by matching the container user to the owner of the mounted data.
- Docker Engine on a Linux server
- A container that writes files to a mounted path
Mounted storage fails with “permission denied” because Linux compares numbers, not names. A bind mount shows host files exactly as they are, with their numeric owner and group, so a container user whose UID does not match cannot write. A named volume is created by Docker and filled from the image on first use, which often avoids the problem. Fix it by matching the container’s user to the data’s owner, not by loosening everything to mode 777.
How it works#
Every file on Linux has an owner UID, a group GID and mode bits. Containers share the host kernel, and the kernel only sees numbers. A user called app inside the image might be UID 1000 there, and the host may have a different user, or none, at UID 1000. Names are irrelevant; the kernel checks the number.
For background on the permission bits themselves, see Linux file permissions explained and file permissions. For persistent data basics, start with Docker volumes and persistent data.
Named volumes: initialized from the image#
Create a volume and mount it:
docker volume create appdata
docker run -d --name app -v appdata:/var/lib/app myimageIf the volume is empty and /var/lib/app already has files in the image, Docker copies them into the volume on first mount, along with their ownership and mode. So if the Dockerfile ran chown app:app /var/lib/app and created that folder, the volume inherits that ownership and the non-root app can write. This copy happens only when the volume is empty. After that, the volume’s content wins even if you rebuild the image. The volume lives under Docker’s data directory, which on Linux is usually /var/lib/docker/volumes/, and you normally leave it alone.
Bind mounts: the host as-is#
A bind mount maps a host path:
mkdir -p /srv/app/data
docker run -d --name app -v /srv/app/data:/var/lib/app myimageNothing is copied. The container sees exactly what is in /srv/app/data, including owner and mode, and it hides whatever the image had at /var/lib/app. If Docker has to create the host folder itself (a case where the path does not exist and -v is used), it creates it owned by root. A container running as UID 1000 then cannot write to it.
Check the numbers from both sides:
ls -ldn /srv/app/data
docker exec app idls -n prints numeric owner and group. id prints the container process’s UID and GID. If they do not line up with the mode bits, writes fail.
Fixes#
Pick one, in this order of preference.
Run the container as the data owner. In Compose:
services:
app:
image: myimage
user: "1000:1000"
volumes:
- /srv/app/data:/var/lib/appWith docker run, use --user 1000:1000. This works when the app does not need root-only paths.
Give the folder to the container’s UID. Find the UID the image runs as with docker run --rm myimage id, then change the ownership of that one folder:
sudo chown 999:999 /srv/app/dataWarning: be exact with the path, and avoid recursive chown -R on a large tree or a directory that holds other people’s files. It rewrites ownership for everything underneath, and a wrong path can break the host. Do not use chmod 777 as a fix. It lets every user on the host write there.
Let Docker own it. Switch to a named volume when you do not need to edit the files from the host. The image’s own ownership is copied in and no host user is involved.
SELinux labels#
On hosts with SELinux enforcing, such as Fedora or RHEL family systems, a bind mount can fail even with correct Unix permissions, because the host files carry the wrong security label. Add a suffix:
docker run -v /srv/app/data:/var/lib/app:z myimage:z applies a label shared between containers, and :Z a private label for one container. Warning: relabeling a broad host path such as your home folder or / changes labels on everything in it, so only use it on a dedicated folder. Ubuntu servers use AppArmor by default and normally do not need these suffixes.
Read-only mounts#
Add :ro to mount a path read-only:
docker run -v /srv/app/config:/etc/app:ro myimageUse it for configuration, certificates and static assets. A container cannot modify them, which limits damage if the app is compromised. If an app tries to write there, you get a read-only file system error, which is by design.
Check it worked#
Write a test file as the container user, then read its owner from the host:
docker exec app sh -c 'touch /var/lib/app/test && ls -ln /var/lib/app/test'
ls -ln /srv/app/data/testBoth commands should succeed, and the numeric owner should match the container’s UID. Remove the test file afterward with rm /srv/app/data/test. For a named volume, confirm it exists with docker volume ls and see its mount details with docker volume inspect appdata.
If writes still fail, the step-by-step diagnosis is in fix permission denied on a Docker volume.
Edge cases and gotchas#
- Names do not map: The username inside the image and on the host are unrelated. Only the numbers matter.
- Root-created folders: A missing bind-mount path is created by Docker as root, so make the folder yourself first with the right owner.
- Volumes hide image data: A bind mount over a folder hides the image’s files at that path. A named volume only copies them when empty.
- Stale volume content: After an image update, an existing volume does not pick up new default files. Migrate them by hand or start a fresh volume.
- Rootless Docker: With rootless mode or user namespace remapping, container UIDs map to a different range on the host, so owners look unexpected in
ls -n. - Windows and macOS: Bind mounts through Docker Desktop pass through a file sharing layer, and ownership and speed behave differently from Linux.
Troubleshooting#
| Symptom | Likely cause | Fix |
|---|---|---|
| Permission denied writing to a bind mount | Container UID does not own the host folder | Use user: to match the owner, or chown that folder |
| Folder owned by root after first run | Docker created the missing path | Create it yourself and set the owner before starting |
| Works on Ubuntu, fails on Fedora | SELinux label missing | Add :z or :Z on a dedicated folder |
| Read-only file system error | Mount has :ro | Remove :ro, or write elsewhere |
| New image defaults not appearing | Volume was already initialized | Copy new files in by hand or use a fresh volume |
Quick reference#
docker run --rm myimage id
ls -ldn /srv/app/data
sudo chown 1000:1000 /srv/app/data
docker run --user 1000:1000 -v /srv/app/data:/data myimage
docker run -v /srv/app/data:/data:z myimage
docker run -v /srv/app/config:/etc/app:ro myimage
docker volume inspect appdataRelated lessons#
- Docker volumes and persistent data for the storage basics.
- Fix permission denied on a Docker volume for a symptom-first checklist.
- Linux file permissions explained for owner, group and mode bits.
- Docker Compose basics for the user and volumes keys.
- Run commands inside a container for checking id and files from inside.
Next#
Continue with Docker logging drivers and log rotation.
Check what you learned
Frequently asked questions
Why do I get permission denied writing to a Docker bind mount?
The container process runs as a numeric user that does not own the host folder. Either chown the folder to that UID and GID, or run the container as the user that owns it.
What is the difference between a bind mount and a volume?
A bind mount maps an exact host path into the container and shows host files as they are. A named volume is created and managed by Docker, and is initialized from the image's content if empty.
How do I run a Docker container as my own user?
Use docker run --user "$(id -u):$(id -g)" or user: in Compose. The app must not need root-only paths for that to work.
What does :ro mean on a Docker mount?
It mounts the path read-only inside the container, so the app cannot change the files. It is good for config files and static content.
Sources
Keep going: guides and tutorials
- Deploy a Docker Compose App Self-Hosted: Web, Redis and Rollback Deploy a Docker Compose app on your own server with Levelrail: a web service and Redis, a bad release that does not take traffic, and a compose rollback.
- Docker Container Restarting in a Loop: 5 Causes, Fixed Five real reasons a Docker container keeps restarting, each reproduced with its exit code, the command that finds it and the fix. Then the same crash on Levelrail.
- Deploy an Express App to a VPS With Docker and Health Checks Deploy a Node.js Express API to your own server with no Dockerfile. Covers the port rule, a health route, secrets, deploy failure codes and rollback.