# Fix permission denied on a Docker volume

> Permission denied on a Docker volume happens because the container's user ID cannot access files owned by a different ID on the host; match the IDs or change the owner.

URL: https://levelrail.com/learn/troubleshooting/fix-permission-denied-on-a-docker-volume/
Updated: 2026-11-27

A container logs `Permission denied` or `EACCES` when it tries to write to a mounted folder. The mount itself works: the folder is there, you can see it, but the process inside cannot change it. The cause is almost always a mismatch between the numeric user ID the container runs as and the owner of the directory on the host. A second cause on Fedora and RHEL hosts is SELinux.

## How it works

Linux decides access from numbers. Every file has an owner user ID and a group ID, and every process has a user ID. User names are only labels that map to numbers through a `passwd` file. A [Docker volume](https://levelrail.com/learn/glossary/#volume)) or bind mount shows the host's files inside the container with the host's numbers unchanged. If the host directory is owned by `root` (UID 0) with mode `755`, and the container runs as UID 1000, the process can read the folder but not write to it. It does not matter that both sides have a user called `app`: the numbers differ.

Bind mounts bring the host's ownership straight in. Named volumes start empty and take their ownership from the image's directory the first time, which usually works but can surprise you when the image runs as a non-root user. The difference is covered in [Docker volumes and persistent data](https://levelrail.com/learn/docker/docker-volumes-and-persistent-data/)).

## Step 1: Read the exact error

```bash
docker logs --tail 50 <container>
```

Look for `EACCES`, `permission denied`, `cannot create directory`, or `unable to open database file`. Note the path. Databases and apps often fail at start-up, so the container may also be in a restart loop, which [fix a Docker container that keeps restarting](https://levelrail.com/learn/troubleshooting/fix-a-docker-container-that-keeps-restarting/)) covers.

## Step 2: Find the user the container runs as

```bash
docker exec <container> id
```

If the container is not staying up, check the image directly:

```bash
docker run --rm --entrypoint id myapp:latest
```

Note the `uid` and `gid`, for example `uid=999(postgres) gid=999(postgres)`. Official images often use a fixed ID such as 999 for a database user, so look it up instead of guessing.

## Step 3: Find the owner on the host

```bash
ls -ln /srv/myapp/data
ls -ldn /srv/myapp/data
```

The `-n` flag prints numeric IDs instead of names, which is what the kernel compares. Compare the owner number and the mode with the container's UID. If the folder shows `0 0` (root) and the container runs as 999, that is your mismatch. If the folder is yours (say 1000) and the container runs as 999, same problem.

## Step 4: Fix the ownership or the user

There are two clean fixes. Pick one.

Option A: change the owner of the host directory to match the container user. This changes only that one directory, so name it exactly:

```bash
sudo chown 999:999 /srv/myapp/data
```

Check the path twice before running it. Avoid `chown -R` on a large tree or on a path such as `/` or `/home` by mistake, since a recursive change on the wrong folder is hard to undo. Use `-R` only on the data directory you mean, and only when the files inside have the wrong owner too.

Option B: run the container as the host owner, using the numbers from `ls -ln`. In Compose:

```yaml
services:
  app:
    image: myapp:latest
    user: "1000:1000"
    volumes:
      - ./data:/app/data
```

With `docker run`, use `--user 1000:1000`. This works for apps that do not need to be root, but some images expect a specific user and break if you override it. Check the image's documentation.

Never fix this with `chmod 777`. It hides the mismatch by giving write access to every user on the host.

## Step 5: Check SELinux on Fedora, RHEL and similar hosts

If the IDs match and you still get denied, and the host runs SELinux in enforcing mode (`getenforce` prints `Enforcing`), the label is the cause. Add a suffix to the mount:

```yaml
    volumes:
      - ./data:/app/data:Z
```

`:z` marks the content as shared between containers, and `:Z` marks it private to this one. Docker relabels the host path, so never use these on a system directory such as `/home` or `/etc`, and only on folders created for the container. Audit messages in `sudo journalctl -t setroubleshoot` or `sudo ausearch -m avc -ts recent` confirm that SELinux blocked it. On Ubuntu and Debian, SELinux is normally off, so skip this step.

## Step 6: Think about rootless Docker

With rootless Docker, the daemon runs as your user and container "root" maps to your own user ID on the host. Other IDs map into a sub-range from `/etc/subuid`. That means a directory owned by your user looks like root inside the container, and a container user such as 999 maps to a high host ID. If you see odd owner numbers on the host, work out the mapping from `/etc/subuid` first, or let the container run as root inside, which is your own user outside. Check the rootless section of the Docker docs before changing ownership by hand.

## Check it worked

Restart the container and test a write from inside:

```bash
docker compose up -d
docker exec <container> sh -c 'touch /app/data/test && echo ok && rm /app/data/test'
```

It should print `ok`, and the application should stop logging `permission denied`. Then check from the host that new files have the owner you expect: `ls -ln /srv/myapp/data`.

## Prevent it

- Create the host directory and set its owner before first start, in your setup script.
- Prefer named volumes for data you do not need to touch from the host.
- Pin the container user in Compose so the IDs are visible and reviewed.

## Edge cases and gotchas

- **Group access:** A directory with mode 770 and a shared group can work for several users. Add the container user to the group with `group_add`.
- **Named volume first-use:** A volume copies the image's folder ownership only when it is empty, so an older volume keeps the old owner.
- **Read-only mounts:** A `:ro` suffix gives `read-only file system`, a different message.
- **Mac and Windows:** Docker Desktop handles file ownership through a file sharing layer, so the rules differ from a Linux server.
- **Init containers:** Some setups run a one-off container as root to `chown` the volume, then start the real one.
- **Entrypoint scripts:** Some images fix ownership at start when run as root, then drop privileges, so overriding `user:` can skip that step.

## Troubleshooting

| Symptom | Likely cause | Fix |
| --- | --- | --- |
| EACCES writing to a bind mount | Host owner differs from container UID | `chown` the exact directory or set `user:` |
| Works as root, fails as app user | Image runs as non-root | Match IDs, do not widen the mode |
| Fails only on Fedora or RHEL | SELinux label | Add `:z` or `:Z` to the mount |
| Old volume keeps wrong owner | Ownership copied only at first use | Fix the owner or recreate the volume after backup |
| Files on host owned by a huge number | Rootless Docker ID mapping | Understand `/etc/subuid` ranges before chowning |
| Permission denied on a socket file | Socket mode or group excludes the user | Add the group with `group_add` |

## Quick reference

```bash
docker exec <container> id
ls -ldn /srv/myapp/data
sudo chown 999:999 /srv/myapp/data
getenforce
```

```yaml
user: "1000:1000"
volumes:
  - ./data:/app/data:Z
```

## Related lessons

- [Docker volumes and persistent data](https://levelrail.com/learn/docker/docker-volumes-and-persistent-data/)): named volumes and bind mounts compared.
- [Linux file permissions explained](https://levelrail.com/learn/server-basics/linux-file-permissions-explained/)): owners, groups and modes in plain terms.
- [Run commands inside a container](https://levelrail.com/learn/docker/run-commands-inside-a-container/)): use docker exec to inspect IDs and files.
- [Docker Compose basics](https://levelrail.com/learn/docker/docker-compose-basics/)): where user and volumes fit in a service.
- [Back up a Postgres database in Docker](https://levelrail.com/learn/deploy-operate/back-up-a-postgres-database-in-docker/)): take a backup before changing volume ownership.

## Next

Continue with [Fix out of memory kills (OOM) on a Linux server](https://levelrail.com/learn/troubleshooting/fix-out-of-memory-kills-oom/)).

## Frequently asked questions

### Why do I get permission denied writing to a Docker volume?

The process in the container runs as a user ID that does not own, and cannot write to, the mounted directory. Linux compares numeric IDs, not names, so the container user and the host owner must match or the mode must allow it.

### How do I find out which user a container runs as?

Run docker exec <container> id, or docker run --rm <image> id for an image. It prints the numeric uid and gid the process uses.

### Is chmod 777 a good fix for Docker volume permissions?

No. It lets every user on the host read and write the data. Set the correct owner with chown on the specific directory, or run the container as a matching user.

### Why does permission denied happen on Fedora or RHEL even when the owner is correct?

SELinux may block the container from the host path. Add :z or :Z to the bind mount so Docker relabels it, as long as the path is not a system directory.


## Sources

- [Docker docs, bind mounts](https://docs.docker.com/engine/storage/bind-mounts/)
