Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

A container cannot read or write a mounted host folder

ErrorPermission denied on accessing host directory in Docker

Short answer

When a bind-mounted host folder is denied inside the container, look at two documented causes first: SELinux on Fedora, RHEL and similar systems blocks the container until the mount carries a :z or :Z label, and a mount flagged ro or readonly can never be written. Fix the flag, then retest.

  • Applies to Docker Engine 27, Docker Compose v2, Fedora 41
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print how the mount is configured. Look for a read-only flag, then check whether SELinux is enforcing.

    docker inspect --format '{{ json .Mounts }}' my-container
    getenforce
  2. 2Match what you see, then jump to its fix

    1. 1getenforce prints Enforcing and the mount has no :z or :ZSELinux blocks the container from the host directoryFix
    2. 2Reading works, every write fails, and the mount shows :ro or readonlyThe mount is read-onlyFix

What does the error look like?

  • Permission denied on accessing host directory in Docker
  • Files in the mounted folder cannot be listed or opened from inside the container
  • Writes to the mounted folder fail while the same folder works on the host

Cause 1 of 2

SELinux blocks the container from the host directory

1Check

SELinux is common on Fedora, RHEL, Rocky and Alma. Docker documents labels for bind mounts: z marks the content as shared among containers, Z as private to one.

getenforce

2Fix

Add the z label if several containers share the folder, or Z if only this container uses it. Use extreme caution: the label changes the host directory itself, so do not label system folders such as your home directory.

docker run -d --name devtest \
  -v "$(pwd)"/target:/app:z \
  nginx:latest

3Verify

List the folder from inside the container.

docker exec devtest ls -l /app

Confirmed by docs.docker.com

Did this fix it?

Cause 2 of 2

The mount is read-only

1Check

Docker lets you mount a bind mount read-only with the readonly or ro option. Search the run command and the compose file for it.

grep -n -E ':ro|readonly|read_only' compose.yaml

2Fix

Remove :ro or the readonly option if the container must write, and recreate the container.

# before
-v "$(pwd)"/target:/app:ro
# after
-v "$(pwd)"/target:/app

3Verify

A test file can be created from inside.

docker exec my-container touch /app/test.txt

Confirmed by docs.docker.com

Did this fix it?

How do I stop it coming back?

  • On SELinux systems add :z or :Z to every bind mount in your run commands and compose files.
  • Use :ro only on mounts that the container must never change, such as config files.
  • Never relabel broad host folders such as your home directory or /etc.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.