Docker Bind Mount "Permission Denied" and Root-Owned Files: Fix the UID/GID Mismatch Without chmod 777
The error comes from a numeric ID mismatch, not from Docker "ignoring" your permissions: the process in the container runs as one UID, the files on the host belong to another, and the kernel compares only the numbers. Fi
The error comes from a numeric ID mismatch, not from Docker "ignoring" your permissions: the process in the container runs as one UID, the files on the host belong to another, and the kernel compares only the numbers. Fix it by running the container as your host UID/GID (user: "1000:1000"), by making the image's user match at build time, or by using a named volume when you don't need the files on the host. chmod -R 777 makes the error go away and is the one answer I'd never leave in a repo.
What does the error actually look like?
There are two mirror-image symptoms, and they share one cause.
Symptom A: the container can't write to your files.
Error: EACCES: permission denied, open '/app/data/cache.json'
PermissionError: [Errno 13] Permission denied: '/app/logs/app.log'
Symptom B: the container writes, and now you can't touch the results.
$ rm -rf node_modules
rm: cannot remove 'node_modules/.bin/tsc': Permission denied
$ ls -ln dist/
-rw-r--r-- 1 0 0 1204 Nov 24 09:12 bundle.js
The -n flag in ls -ln is the useful diagnostic. It prints numeric IDs, which is what the kernel actually compares. Owner 0 0 means the container ran as root and created the file. A bind mount shares the host directory's inodes directly, so the ownership the container sets is the ownership you get on the host.
Takeaway: when permissions look "wrong", compare numeric IDs with ls -ln on the host and id inside the container, because names mean nothing across the boundary.
Why does it happen? (Linux vs. Docker Desktop)
On a Linux host with the native Docker engine, a bind mount has no translation layer. If the host directory is owned by UID 1000 with mode 755, a container process running as UID 1001 (or a non-root node user that happens to be 1000 in one image and 1001 in another) gets "other" permissions: read-only.
On Docker Desktop for Mac and Windows, the file-sharing layer usually presents files to the container with ownership that makes most of this invisible. That is why a setup that works on a teammate's Mac fails on the Linux CI runner or a Linux laptop. If a bug reproduces only on Linux, suspect this one first. I haven't seen the exact mapping documented as a stable contract, so I wouldn't rely on it either way; treat "works on my Mac" as no evidence.
Takeaway: Linux enforces raw UID/GID equality on bind mounts, and Docker Desktop's file-sharing layer hides the mismatch until the code reaches a Linux machine.
Which fix should you use?
| Situation | Fix | Trade-off |
|---|---|---|
| Dev container that edits your source tree | Run as host UID/GID via user:
|
Needs the IDs passed in; image must tolerate an arbitrary UID with no passwd entry |
| Image you control and build locally | Create the user at build time with ARG UID/GID
|
Image is tied to one UID; rebuild per developer |
| Data you don't need to read on the host | Named volume | Not browsable as a normal folder; separate backup story |
| Rootless Podman | --userns=keep-id |
Podman-specific behavior |
| Shared CI cache directory |
chown once in an init step |
Extra step; must run as root briefly |
Takeaway: choose by who needs to read the files: if only the container does, use a named volume; if you also edit them, match the UIDs.
How do you run the container as your host UID/GID?
The smallest fix is one line in Compose. The catch that trips people: in bash and zsh, UID is a shell variable but usually not exported, so Compose never sees it and silently substitutes an empty string. Put the values in a .env file instead.
# Run once per machine; .env should be gitignored
printf 'HOST_UID=%s\nHOST_GID=%s\n' "$(id -u)" "$(id -g)" > .env
# compose.yaml
services:
app:
image: node:22-slim
user: "${HOST_UID:-1000}:${HOST_GID:-1000}"
working_dir: /app
volumes:
- ./:/app
command: npm run dev
The :-1000 default keeps the file working on a fresh clone. Verify it did what you expect:
docker compose run --rm app id
# uid=1000 gid=1000 groups=1000
docker compose run --rm app touch /app/probe && ls -ln probe
The limitation: a process running as a UID with no entry in the image's /etc/passwd has no home directory and no username. Tools that write to $HOME (npm's cache, pip, git) can fail with a different EACCES on /.npm or /root/.cache. Set HOME to a writable path to avoid it:
environment:
HOME: /tmp
npm_config_cache: /tmp/.npm
Takeaway: user: plus a .env file fixes both symptoms, and the follow-up bug is almost always an unwritable $HOME.
What if you build the image yourself?
Create the user with the host's IDs at build time, so the account, home directory, and file ownership all line up.
FROM python:3.13-slim
ARG UID=1000
ARG GID=1000
RUN groupadd --gid "$GID" app \
&& useradd --uid "$UID" --gid "$GID" --create-home app
USER app
WORKDIR /home/app/src
services:
app:
build:
context: .
args:
UID: ${HOST_UID:-1000}
GID: ${HOST_GID:-1000}
volumes:
- ./:/home/app/src
One gotcha: some base images already own UID 1000 (the official node images ship a node user at 1000, for example), so useradd --uid 1000 fails with "UID 1000 is not unique". Reuse the existing user instead of creating a second one, or pick the base image's user and pass its ID through.
If you want the same result without owning a Dockerfile, Podman in rootless mode is the cleaner route: podman run --userns=keep-id maps your host user to the same UID inside the container, so bind mounts behave the way people expect. Its drawback is that Compose-file compatibility is good but not perfect, so check your file before switching.
Takeaway: build-time ARG IDs make the image self-consistent, but they bake one developer's UID into a layer, so reserve them for local dev images.
When is a named volume better?
If the container is the only reader and writer (database data, node_modules, build caches), skip the host path entirely.
services:
app:
volumes:
- ./:/app
- node_modules:/app/node_modules # keeps container-built deps off the host
volumes:
node_modules:
Named volumes are populated from the image's contents on first use, preserving the ownership baked into the image, which sidesteps the mismatch. This also stops a Linux-built node_modules from landing in a macOS checkout. The cost is visibility: you inspect the data with docker run --rm -v node_modules:/v alpine ls -ln /v rather than your file manager, and you must remember to include it in backups.
Takeaway: anything you never need to open on the host belongs in a named volume, which removes the UID question entirely.
FAQ
Why are files created by Docker owned by root on my host?
The container process ran as root (UID 0), and a bind mount shares the host's inodes directly, so new files carry UID 0. Run the container with --user "$(id -u):$(id -g)" or user: in Compose to create files as yourself.
Is chmod 777 a safe fix for Docker volume permissions?
No. It lets every local user and every process in any container write to the directory, and it hides the real mismatch so it returns on the next machine. Match the UID/GID instead.
How do I fix "permission denied" on a bind mount in docker compose?
Run ls -ln on the host path and id in the container, then set user: "<host-uid>:<host-gid>" on the service so the numbers match. If the error then moves to a cache or home directory, set HOME to a writable path like /tmp.
Bottom line
For a source tree you edit from the host, run the service with your own UID/GID through a gitignored .env, and set HOME somewhere writable. For data only the container touches, use a named volume and stop thinking about ownership. If you build your own dev image, bake the IDs in with build args, and if you are on Linux and want the least friction, rootless Podman with --userns=keep-id is worth an afternoon. Never ship chmod 777 as the fix.
Related reading
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.