Chapter 2 · Lesson 19 of 20
Docker

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.

By the end you will

Pick bind mount or volume and fix write errors by matching the container user to the owner of the mounted data.

Time
14 min
Level
Intermediate
Check
4 questions
You need
  • 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:

bash
docker volume create appdata
docker run -d --name app -v appdata:/var/lib/app myimage

If 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:

bash
mkdir -p /srv/app/data
docker run -d --name app -v /srv/app/data:/var/lib/app myimage

Nothing 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:

bash
ls -ldn /srv/app/data
docker exec app id

ls -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:

yaml
services:
  app:
    image: myimage
    user: "1000:1000"
    volumes:
      - /srv/app/data:/var/lib/app

With 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:

bash
sudo chown 999:999 /srv/app/data

Warning: 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:

bash
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:

bash
docker run -v /srv/app/config:/etc/app:ro myimage

Use 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:

bash
docker exec app sh -c 'touch /var/lib/app/test && ls -ln /var/lib/app/test'
ls -ln /srv/app/data/test

Both 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#

SymptomLikely causeFix
Permission denied writing to a bind mountContainer UID does not own the host folderUse user: to match the owner, or chown that folder
Folder owned by root after first runDocker created the missing pathCreate it yourself and set the owner before starting
Works on Ubuntu, fails on FedoraSELinux label missingAdd :z or :Z on a dedicated folder
Read-only file system errorMount has :roRemove :ro, or write elsewhere
New image defaults not appearingVolume was already initializedCopy new files in by hand or use a fresh volume

Quick reference#

bash
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 appdata

Next#

Continue with Docker logging drivers and log rotation.

Check what you learned4 questions
1A container runs as UID 1000. A bind-mounted host folder is owned by root with mode 755. What happens when the app writes to it?
2When does a named volume get filled with the image's files at that path?
3What does the :Z suffix on a bind mount do on an SELinux host?
4Which Compose key runs a service as a specific numeric user and group?

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

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios