Fixing Docker bind mount permission errors and volume traps
Why Docker bind mounts trigger Permission denied errors on Linux, how to fix UID mismatches with gosu, and why databases belong on named volumes.
Mounting a local directory into a Docker container often fails with
Permission deniedor leaves root-owned files behind on your host. Here is how Linux UID checks cause that mismatch, how to fix it with--useror agosuentrypoint, and why databases should always use named volumes.
The local bind mount permission trap
If you’ve set up a local Docker dev environment, you’ve probably seen errors like these right after adding a bind mount:
1
2
[ERROR] 2026-06-10 12:15:30 - java.io.FileNotFoundException: /app/logs/application.log (Permission denied)
npm ERR! Error: EACCES: permission denied, mkdir '/app/node_modules/.cache'
You mount -v $(pwd):/app for hot reloading or -v $(pwd)/logs:/app/logs to inspect logs on your host, and the app immediately refuses to start because it can’t write to the mounted directory.
The opposite failure is just as annoying. When a container runs as root and writes files into a bind-mounted directory, those files end up owned by root on your host filesystem. Later, your IDE can’t edit them, git clean fails, and you’re stuck running sudo rm -rf just to clean up your own workspace.
Without understanding how Docker storage and Linux file permissions interact, it’s tempting to slap chmod 777 on the directory and move on. That opens a glaring security hole and doesn’t fix root-owned output files. Below, I break down Docker’s three storage options, why the kernel rejects bind-mounted writes, and the patterns I use to solve it cleanly.
Three Docker storage types and when to use each
A container’s default filesystem sits on a writable layer managed by the Union File System (OverlayFS). Because that layer disappears when the container is removed, anything you want to keep or share with the host requires a mount. Docker gives you three mechanisms.
flowchart TD
subgraph Host["Host Machine"]
NV["Named Volume (/var/lib/docker/volumes/)"]
BM["Bind Mount (/home/user/project)"]
TMP["tmpfs Mount (Host RAM)"]
end
subgraph Container["Container"]
NV_Mount["/var/lib/postgresql/data"]
BM_Mount["/app/src"]
TMP_Mount["/tmp/secure-keys"]
end
NV -->|"Isolated native I/O"| NV_Mount
BM -->|"Two-way host sync"| BM_Mount
TMP -->|"In-memory ephemeral"| TMP_Mount
Comparing the three storage types
| Feature | Named Volume | Bind Mount | tmpfs Mount |
|---|---|---|---|
| Storage location | Docker-managed area (/var/lib/docker/volumes/) | Any absolute path on the host filesystem | Host system memory (RAM) |
| Management | Managed via Docker CLI and daemon | Directly accessible by host processes and IDEs | Allocated by the Docker daemon |
| Permissions | Docker preserves volume ownership and permissions | Enforces the host directory’s exact UID/GID | Uses in-memory filesystem permissions |
| I/O performance | Native Linux speed; avoids macOS/WSL2 host-share overhead | Slower on macOS/Windows due to VM file-sharing layers | Fastest (memory-backed, no disk I/O) |
| Best use cases | Database storage, persistent state, backups | Local source code sync, config injection, build outputs | Secrets (API keys, tokens), ephemeral caches |
When not to use bind mounts: Don’t use bind mounts for stateful databases or message brokers in local dev or production. Use bind mounts for live source code and config files you edit on the host, and put PostgreSQL, MySQL, Redis, or Kafka data on named volumes.
Use bind mounts for live source code and host config files. Use named volumes for databases and stateful services where I/O speed and file permissions matter.
Why Permission denied happens at the kernel level
To understand bind mount errors, remember that a container isn’t a separate virtual machine. It’s a regular Linux process isolated on the host kernel with namespaces and cgroups.
The kernel checks numeric UIDs, not usernames
When the Linux Virtual File System (VFS) checks file access, it ignores string usernames like kimnamju, node, or spring. It only compares the process’s numeric Effective UID/GID against the numeric Owner UID/GID stored on the file’s inode.
sequenceDiagram
autonumber
participant HostUser as Host user (UID 1000)
participant Kernel as Linux Kernel VFS
participant ContainerProc as Container process (UID 1001)
HostUser->>Kernel: Create ./logs on host (Owner UID 1000, mode 755)
Note over Kernel: Directory mode rwxr-xr-x<br/>Only UID 1000 can write
ContainerProc->>Kernel: Write /app/logs/log.txt (Process UID 1001)
Kernel->>Kernel: UID 1001 != Owner 1000<br/>Falls back to Other (r-x, no write)
Kernel-->>ContainerProc: Return EACCES (13: Permission denied)
This creates two classic failure modes:
- The container runs as a non-root user (for example, UID 1001):
- Your host directory is owned by your host user (UID 1000) with mode
drwxr-xr-x. - The container process has UID 1001, so the kernel evaluates it under the
Otherpermission bits (r-x). - Because
Otherlacks write (w) permission, the app immediately fails withPermission deniedorEACCES.
- Your host directory is owned by your host user (UID 1000) with mode
- The container runs as the default
rootuser (UID 0):- The container process writes freely into the bind-mounted directory.
- Every file it creates is stamped with UID 0 (
root) on the host filesystem. - Your non-root host account (UID 1000) can no longer edit or delete those files without
sudo.
Three ways to fix UID and GID mismatches
You can solve the UID mismatch in three ways depending on whether you control the image and whether your team shares it across Linux and macOS.
Approach 1: Runtime UID mapping with --user
The fastest fix is passing your current host UID and GID into the container at startup:
1
2
3
4
5
6
# Run a single container with the host user's UID and GID
docker run -d \
--name backend-api \
--user "$(id -u):$(id -g)" \
-v "$(pwd)/logs:/app/logs" \
my-backend-image:latest
In compose.yaml, pass them through environment variables:
1
2
3
4
5
6
services:
backend-api:
image: my-backend-image:latest
user: "${UID:-1000}:${GID:-1000}"
volumes:
- ./logs:/app/logs
Limits of
--user: If your host UID (say,1000or501) doesn’t exist in the container’s/etc/passwd, tools that look up the current user or home directory (~) can fail withI have no name!or crash while creating caches under/home/.../.cache. Processes launched this way also can’t bind privileged ports like 80 or 443.
Approach 2: Build-time user creation with ARG
You can pass the host UID and GID at build time so the image creates a real entry in /etc/passwd:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
FROM node:20-alpine
# Accept host UID and GID at build time (default: 1000)
ARG USER_ID=1000
ARG GROUP_ID=1000
# Reuse or create a matching group and user
RUN if getent group ${GROUP_ID} ; then \
groupmod -g ${GROUP_ID} node ; \
else \
groupadd -g ${GROUP_ID} appgroup ; \
fi && \
if getent passwd ${USER_ID} ; then \
usermod -u ${USER_ID} -g ${GROUP_ID} node ; \
else \
useradd -m -u ${USER_ID} -g ${GROUP_ID} appuser ; \
fi
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
# Switch to the matching non-root user
USER ${USER_ID}:${GROUP_ID}
CMD ["node", "server.js"]
- Pros: The UID exists in
/etc/passwd, so$HOMEand tool caches work normally. - Cons: If teammates run different host UIDs (macOS defaults to
501, Linux to1000), everyone has to build their own image and you lose shared Docker layer caching across the team.
Approach 3: Dynamic handoff with entrypoint.sh and gosu
This is the pattern official images like PostgreSQL and Redis use, and it’s the most reliable option for shared team images. The container starts briefly as root to align appuser’s UID/GID with the host and fix ownership on mounted paths, then permanently drops privileges before launching the actual app.
flowchart TD
A["Container starts as root (PID 1 entrypoint)"] --> B["Read LOCAL_UID and LOCAL_GID env vars"]
B --> C["Remap appuser UID/GID to match host"]
C --> D["Run chown on mounted dir (/app/logs)"]
D --> E["Execute exec gosu appuser"]
E --> F["App runs as non-root PID 1 (receives SIGTERM)"]
Why gosu instead of su or sudo?
Both su and sudo fork a child process and sit in the middle, which mangles TTY signal forwarding and can leave zombie processes behind. gosu is a tiny Go binary that calls execve directly, replacing the PID 1 shell process with your application under the target user. That way, SIGTERM goes straight to your app during shutdown.
1. Dockerfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
FROM ubuntu:24.04
# Install gosu and CA certificates
RUN apt-get update && apt-get install -y --no-install-recommends \
gosu \
ca-certificates \
&& rm -rf /var/lib/apt/lists/*
# Create the default non-root application user
RUN groupadd -g 1000 appgroup && \
useradd -u 1000 -g appgroup -m -s /bin/bash appuser
WORKDIR /app
# Copy the entrypoint script and make it executable
COPY docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh
RUN chmod +x /usr/local/bin/docker-entrypoint.sh
COPY . /app
ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"]
CMD ["python3", "-m", "http.server", "8080"]
2. docker-entrypoint.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/bin/bash
set -e
# Read host UID/GID from env vars (fallback to 1000)
USER_ID=${LOCAL_UID:-1000}
GROUP_ID=${LOCAL_GID:-1000}
# 1. Align appgroup GID with the host GID
CURRENT_GID=$(getent group appgroup | cut -d: -f3)
if [ "$CURRENT_GID" != "$GROUP_ID" ]; then
groupmod -o -g "$GROUP_ID" appgroup
fi
# 2. Align appuser UID with the host UID
CURRENT_UID=$(id -u appuser)
if [ "$CURRENT_UID" != "$USER_ID" ]; then
usermod -o -u "$USER_ID" appuser
fi
# 3. Fix ownership only on the mounted write paths
if [ -d "/app/logs" ]; then
chown -R appuser:appgroup /app/logs
fi
# 4. Drop privileges and replace PID 1 with the app process
exec gosu appuser "$@"
3. compose.yaml
1
2
3
4
5
6
7
8
9
10
11
services:
backend-service:
build: .
environment:
# Pass host shell UID and GID into the container
LOCAL_UID: ${UID:-1000}
LOCAL_GID: ${GID:-1000}
volumes:
- ./logs:/app/logs
ports:
- "8080:8080"
With this setup, one shared image works out of the box on macOS (UID 501) and Linux (UID 1000 or 1001) without Permission denied errors or root-owned host files.
Why databases belong on named volumes
I still see Compose files that bind-mount a local folder directly into a PostgreSQL or MySQL data directory:
1
2
3
4
5
6
# Anti-pattern: bind-mounting a host directory for database storage
services:
postgres:
image: postgres:16-alpine
volumes:
- ./pgdata:/var/lib/postgresql/data # Avoid this
This causes three serious problems in dev and production:
- Severe I/O slowdown on macOS and Windows: Docker Desktop routes bind mounts through a virtualized file-sharing layer like VirtioFS or gRPC-FUSE. Relational databases call
fsync()and flush Write-Ahead Logs (WAL) constantly, so I/O throughput can drop by an order of magnitude compared to native Linux storage. - PostgreSQL’s strict
0700permission check: PostgreSQL refuses to start (FATAL: data directory has wrong ownership or permissions) ifPGDATAisn’t strictly0700and owned by thepostgresuser. Host bind mounts frequently break that invariant. - Filesystem metadata mismatches: Differences in inode locking and case sensitivity between host filesystems (APFS, NTFS) and Linux ext4/xfs can corrupt database files under heavy write load.
Declare a named volume instead:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# Recommended: use a Docker-managed named volume for database files
services:
postgres:
image: postgres:16-alpine
environment:
POSTGRES_DB: myapp
POSTGRES_USER: myuser
POSTGRES_PASSWORD: secretpassword
volumes:
- postgres_data:/var/lib/postgresql/data
ports:
- "5432:5432"
volumes:
postgres_data:
driver: local
Named volumes live inside Docker’s native Linux storage area (inside the Linux VM on Docker Desktop), giving you fast disk I/O and clean Linux ownership semantics.
Backing up named volumes and final checklist
The most common pushback against named volumes is that you can’t see the files in your project folder when you want a backup. You don’t need a bind mount for that—a one-shot Alpine container with --rm can archive and restore any named volume in one command.
Back up a named volume to a tar archive
1
2
3
4
5
6
# Archive postgres_data into pg_backup.tar.gz in the current host directory
docker run --rm \
-v postgres_data:/volume-data:ro \
-v "$(pwd)":/backup \
alpine \
tar -czvf /backup/pg_backup.tar.gz -C /volume-data .
-v postgres_data:/volume-data:romounts the target volume read-only.-v "$(pwd)":/backupmounts your current host directory as the destination.--rmdeletes the helper container as soon astarfinishes.
Restore a tar archive into a new named volume
1
2
3
4
5
6
7
8
9
# 1. Create a fresh named volume
docker volume create postgres_data_restored
# 2. Extract the archive into the new volume
docker run --rm \
-v postgres_data_restored:/volume-data \
-v "$(pwd)":/backup \
alpine \
tar -xzvf /backup/pg_backup.tar.gz -C /volume-data
Production backup tip: Tarring a live database volume while writes are in flight can leave you with an inconsistent WAL state. Prefer
pg_dumpormysqldumpfor running databases, or stop the database container (docker compose stop postgres) before taking a raw volume snapshot.
Storage and permissions checklist
Before shipping your Docker setup, run through these four checks:
- Hot-reloaded source code or host-accessible logs: Use a bind mount, and wire
LOCAL_UID/LOCAL_GIDwith agosuentrypoint if multiple developers share the image. - Stateful databases and brokers (PostgreSQL, MySQL, Redis, Kafka): Always use a named volume—never bind-mount a host directory into
PGDATAor similar data paths. - Short-lived secrets or throwaway caches: Use a tmpfs mount so nothing touches disk and data vanishes on container stop.
- No
chmod 777workarounds: Match numeric UIDs between host and container instead of opening world-writable permissions.