Sub topic 13
Debugging Volumes, Bind Mounts, and File Permissions
This chapter assumes you can run containers with -v or --mount, read docker inspect output, and use docker exec (covered earlier in this tutorial). You should also know that Linux files have a numeric owner (UID) and group (GID) and a permission mode.
Mount problems usually look like application problems. The app says "permission denied", "no such file", or "database is empty", and the cause is that the container sees different files than you expect. The method is to check what is mounted, then who owns it, then who the process runs as.
The three kinds of mounts
| Kind | Example | Where data lives | Typical failure |
|---|---|---|---|
| Named volume | `-v pgdata:/var/lib/postgresql/data` | Managed by Docker under its data directory | Stale data from an earlier run, wrong ownership |
| Bind mount | `-v "$PWD/conf:/etc/app"` | A host path you choose | Path missing, shadowed image files, UID mismatch |
| tmpfs | `--tmpfs /tmp` | Host memory | Data disappears on stop, size limit reached |
Anything written outside a mount goes into the container's writable layer and is lost when the container is removed. Many "my data vanished" reports are caused by the application writing to a path that is not mounted.
Step 1: See what is actually mounted
Do not trust the command you remember typing. Ask the daemon:
docker inspect --format '{{json .Mounts}}' webEach entry has a Type, a Source (the host path or volume), a Destination (the path inside the container), and an RW flag. Check three things:
- The
Destinationis the exact path the application uses./dataand/app/dataare different. RWistrueif the app must write there.- The
Sourceis the host directory or volume you intended.
To work from the volume side:
docker volume ls
docker volume inspect pgdata
docker ps -a --filter volume=pgdata --format 'table {{.Names}}\t{{.Status}}'The last command lists every container, running or stopped, that uses pgdata. Two containers writing to one volume is a common source of corruption and lock errors.
In Compose, docker compose config shows the volume definitions after variable substitution. This is the quickest way to catch a typo in ${DATA_DIR} that left the source path empty.
Step 2: Look from inside the container
Compare what the container sees with what you expect:
docker exec web ls -lan /app/uploads
docker exec web id
docker exec web touch /app/uploads/probe && echo writableThe -n flag prints numeric UID and GID instead of names. This matters because the kernel only compares numbers. A user called app inside the container and a user called app on the host are unrelated unless the numbers match.
If the container exits before you can use exec, start a throwaway container with the same mounts and a different command:
docker run --rm -it --entrypoint sh \
-v "$PWD/uploads:/app/uploads" \
--user 1000:1000 \
myimage:latestCommon failures
Permission denied from a UID mismatch
This is the most frequent case. The process runs as UID 1000, but the mounted directory is owned by UID 0 (root) with mode 755, so only root can write.
docker exec web id
docker exec web ls -ldn /app/uploadsIf id prints uid=1000 and ls prints drwxr-xr-x 2 0 0, you have found the problem. There are three common fixes:
- Change ownership on the host for a bind mount:
sudo chown -R 1000:1000 ./uploads. - Run the container as your host user during development:
docker run --user "$(id -u):$(id -g)" .... In Compose, setuser:on the service. - Fix ownership inside a volume with a one-off container:
docker run --rm -v pgdata:/data alpine chown -R 999:999 /dataAvoid chmod 777 as a fix. It hides the mismatch and leaves the files writable by every user on the host.
Bind mount source created as root
With -v, if the host path does not exist, Docker creates it as a directory owned by root. A typo such as ./uplaods therefore produces an empty, root-owned directory and no error. If you meant to mount a file, you now have a directory where the app expects a file, and it fails with errors like "is a directory".
The --mount syntax is stricter. It refuses to start when a bind source is missing:
docker run --rm \
--mount type=bind,source="$PWD/conf/app.toml",target=/etc/app/app.toml,readonly \
myimage:latest cat /etc/app/app.tomlPrefer --mount while debugging, because it turns silent mistakes into immediate errors.
A mount hides files from the image
A bind mount replaces whatever the image has at the destination. If the image ships /app/node_modules and you bind-mount your project at /app, the container sees your host directory instead, and node_modules is gone if it does not exist on the host. Confirm with:
docker run --rm myimage:latest ls /app
docker run --rm -v "$PWD:/app" myimage:latest ls /appIf the two listings differ, the mount is shadowing the image.
Named volumes behave differently. When you mount an empty named volume over a path that has content in the image, Docker copies the image content into the volume first, along with its ownership. A volume that already has data is never refreshed. If you rebuild the image with new default files but reuse an old volume, the container keeps the old files. To test with a clean slate, use a new volume name, or remove the old one with docker volume rm after you have confirmed it holds nothing you need.
Read-only mounts and filesystems
:ro on a mount, readonly in --mount, or --read-only on the container all produce "Read-only file system" errors. Check the RW field in .Mounts. For --read-only, inspect .HostConfig.ReadonlyRootfs:
docker inspect --format 'rootfs-readonly={{.HostConfig.ReadonlyRootfs}}' webWith a read-only root filesystem, the app still needs writable locations for temporary files. Add --tmpfs /tmp or a volume for those paths.
SELinux and platform differences
On hosts with SELinux enforcing (Fedora, RHEL), a bind mount can fail with "permission denied" even when UIDs and modes are correct. Add :z (shared among containers) or :Z (private to this container) to relabel the content, as in -v "$PWD/data:/data:Z". Never relabel system directories such as your home directory or /usr.
On Docker Desktop (macOS, Windows), the host path must be in a shared location, and file ownership shown inside the container is translated by the file-sharing layer. Rootless Docker maps UIDs through a user namespace, so UID 0 in the container is your own user on the host. Numbers you see on the host will not match the numbers inside.
Data written to the wrong place
If a volume looks empty but the app seems to work, it may be writing outside the mount. List what the container added to its writable layer:
docker diff webPaths that appear here, such as A /app/data/db.sqlite, are not in your volume. Fix the application's configured data path or the mount destination so the two agree.
A decision path
Worked example
A Node container exits with EACCES: permission denied, open '/app/uploads/a.png'.
docker inspect --format '{{json .Mounts}}' web
docker exec web id
docker exec web ls -ldn /app/uploadsThe mount is present and RW is true. id prints uid=1000(node), and ls prints drwxr-xr-x 2 0 0. The directory is owned by root because Docker created the missing host path. Running sudo chown -R 1000:1000 ./uploads on the host and retrying the upload fixes it. To avoid a repeat, create the directory before starting the container, or switch to --mount.
Exercise
A container runs as UID 1001 and fails to write to a named volume mounted at /var/lib/app. docker inspect shows RW is true and the destination is correct. What two commands would you run next to confirm an ownership problem, and how would you fix it without using chmod 777?
Show answer
Run docker exec web id to get the numeric UID and docker exec web ls -ldn /var/lib/app to get the directory's numeric owner and mode. If the owner is 0 (or any UID other than 1001) and the mode does not allow writes for others, fix ownership with a one-off container: docker run --rm -v VOLUME:/data alpine chown -R 1001:1001 /data, where VOLUME is the volume name. Alternatively, change which user the container runs as to match the volume owner.