Difficulty: Advanced
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?
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.
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:
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.
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.--build-arg UID=$(id -u) and create the user with that UID in the Dockerfile. Works well for team dev images.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.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.Avoid chmod -R 777. It silences the error by giving every user on the server write access to the data.
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.
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.
# 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.
# 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.
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.
UID/GID mapping, Bind mounts, userns-remap, Rootless Docker, SELinux labels