Bind Mount Permissions

Difficulty: Advanced

Question

You bind-mount a host directory into a container on a Linux server and get "Permission denied". Why does this happen and how do you fix it properly?

Answer

Linux checks file access using numeric UIDs and GIDs, not user names, and a bind mount exposes host files with their host ownership unchanged. If the container process runs as UID 1001 but the files belong to host UID 1000 with mode 755, the container cannot write to them. The fix is to make the numeric IDs line up (run the container as the host user, or build the image user with a matching UID), or use user namespaces or rootless Docker so the kernel maps IDs for you.

Why names do not help

Inside the container node might be UID 1000, and on the host ubuntu might also be UID 1000; then it works, by coincidence. The kernel never compares names, only numbers. Common mismatches:

  • The image creates an app user with UID 1001 or 10001 (common in hardened images), but your files are owned by UID 1000.
  • The container runs as root (UID 0) and creates files on the mount, which then show up on the host as owned by root and cannot be edited or deleted by your normal user.
  • The host directory does not exist, so Docker creates it owned by root before the container starts.

Named volumes behave differently: on first use, Docker copies the image directory's content and ownership into the empty volume. Bind mounts never do this.

Fixes, from simplest to most robust

  1. Run as the host user: docker run --user "$(id -u):$(id -g)". Ideal for dev tooling such as running npm install or a code generator against your working tree. The UID may have no entry in /etc/passwd, so tools that need a home directory may complain; set HOME=/tmp if so.
  2. Build the image with a matching UID: pass --build-arg UID=$(id -u) and create the user with that UID in the Dockerfile. Works well for team dev images.
  3. Fix ownership at startup: an entrypoint that starts as root, runs chown on the mount, then drops privileges with gosu or su-exec. Flexible, but chown -R on a large tree is slow and it changes ownership on the host too.
  4. User namespaces (userns-remap): the daemon maps container UID 0 to an unprivileged host UID range. Strong isolation, but it is daemon-wide and complicates host bind mounts, since container root is no longer host root.
  5. Rootless Docker or Podman: container root maps to your own user, so files written as root in the container appear owned by you on the host.

Avoid chmod -R 777. It silences the error by giving every user on the server write access to the data.

The SELinux trap

On Fedora, RHEL, Rocky and similar hosts with SELinux enforcing, you can get Permission denied even when UIDs match. The container process runs with a confined SELinux label that cannot read arbitrary host files. Add :z (shared label) or :Z (private label) to the mount, for example -v ./data:/data:Z. Never relabel system directories like /home or /usr, since that can break the host.

Docker Desktop is different

On macOS and Windows, Docker Desktop runs containers in a Linux VM and shares host folders through its file-sharing layer, which presents files so the container user can usually write them. That is why "it works on my Mac" but fails on the Linux CI runner or the production VM.

How the kernel decides a bind mount write. Ownership is compared as numbers, so the IDs must match or be mapped.
  1. Process runs as UID 1001: Set by USER in the image or by --user.
  2. Writes to /app/data: A bind mount of a host directory.
  3. Host files owned by UID 1000: Docker does not translate ownership.
  4. Kernel compares numbers: 1001 is not 1000 and the mode is 755.
  5. EACCES: Permission denied: Fix by matching or mapping the IDs.

Code examples

Diagnose, then run as your host user

# Diagnose: compare numeric owners on both sides
ls -ln ./data
docker run --rm -v "$PWD/data:/data" my-app id

# Fix for one-off tools: run with the host UID and GID
docker run --rm \
  --user "$(id -u):$(id -g)" \
  -e HOME=/tmp \
  -v "$PWD:/app" -w /app \
  node:22-alpine npm install

ls -ln shows numeric owners, id shows the container user, and --user makes new files land on the host owned by you.

Build the image user with a matching UID

# Dockerfile
FROM python:3.12-slim
ARG UID=1000
ARG GID=1000
RUN groupadd -g "$GID" app && useradd -m -u "$UID" -g app app
USER app
WORKDIR /home/app/src

# Build with your own IDs:
# docker build --build-arg UID=$(id -u) --build-arg GID=$(id -g) -t app-dev .

Baking the developer's UID into a dev image removes the mismatch without running anything as root.

SELinux-safe mount in Compose

services:
  api:
    image: app-dev
    user: "1000:1000"
    volumes:
      # :Z relabels the directory for this container only
      - ./data:/home/app/data:Z

On SELinux hosts the :Z suffix fixes denials that UID matching alone cannot solve.

Key points

  • Permissions are checked on numeric UID/GID; Docker does not translate ownership on bind mounts.
  • Named volumes copy image ownership on first use; bind mounts never do.
  • Prefer --user $(id -u):$(id -g) or a build-arg UID over chmod 777.
  • On SELinux hosts, add :z or :Z to the mount even when UIDs match.
  • userns-remap and rootless mode map container root to an unprivileged host user.

Common follow-up questions

  • How do you check the UID a container process is running as?
  • What is the difference between chown and chmod?
  • What are the trade-offs of enabling userns-remap on a shared host?
  • Why do files created by a root container show up as root-owned on the host?

Interview tips

  • Say "numeric UID" early; it shows you understand the root cause, not just the workaround.
  • Rank the fixes and explain why chmod 777 is the wrong one.
  • Mentioning the SELinux :Z case signals real experience on RHEL-family servers.

Concepts covered

UID/GID mapping, Bind mounts, userns-remap, Rootless Docker, SELinux labels

More Docker & Containers questions