Sub topic 13
Debugging Volumes, Bind Mounts, and File Permissions
This chapter assumes you know how to use docker exec and docker inspect. It also assumes you know Linux file ownership (user IDs, group IDs, and mode bits). Storage problems usually show up as missing files, "permission denied", or data that disappears. You can find the cause by comparing what the container sees with what the host has.
Check what is mounted
Start with the container's own record of its mounts:
docker inspect --format '{{range .Mounts}}{{.Type}} {{.Source}} -> {{.Destination}} rw={{.RW}}{{"\n"}}{{end}}' webEach line shows the mount type (bind or volume), the host source, the path inside the container, and whether it is writable. Many problems are visible here. The destination may be wrong, or rw=false may be set.
Common failure patterns
| Symptom | Likely cause | Check |
|---|---|---|
| Directory is empty inside the container | A bind mount hides the image's files at that path, or the host path is wrong | Compare `docker exec web ls /path` with the host directory |
| `Permission denied` writing to a mount | The container user's UID does not match the owner of the files | `docker exec web id` and `docker exec web ls -ln /path` |
| `Read-only file system` | The mount was made with `:ro`, or the container uses `--read-only` | `rw=` in the inspect output |
| Host directory is owned by root | Docker created a missing host path for a `-v` bind mount | `ls -ln` on the host |
| Data is gone after recreating the container | The data was in the container's writable layer, not in a volume | `docker diff web` |
A bind mount replaces whatever the image has at the destination. A new empty named volume is different, because Docker copies the image's content into it the first time it is used.
Find the UID mismatch
Permissions are enforced by numeric IDs, not names. Compare the container user with the file owner. The -n flag prints numbers, so names that differ between the host and the image cannot mislead you.
docker exec web id
docker exec web ls -ln /dataIf the process runs as UID 1000 but /data belongs to UID 0, writes fail. You can fix this in three ways:
- Change the owner on the host:
sudo chown -R 1000:1000 ./data. - Run the container as your own user with
--user. - Set ownership in the image or entrypoint, for example with
chownin the Dockerfile for paths that live in the image.
This test runs a throwaway container as your host user and tries a write:
docker run --rm -v "$PWD/data:/data" --user "$(id -u):$(id -g)" alpine:3 touch /data/testIf this succeeds and your real container fails, the image's user is the problem.
Special environments
On SELinux hosts, a bind mount may fail with "permission denied" even when the owner is correct. Add :z (shared) or :Z (private) to the mount, as in -v "$PWD/data:/data:Z". With Docker Desktop, the host path must be in a shared location, and ownership may appear different from what a native Linux host shows.
Exercise
A container running as UID 1000 gets "Permission denied" when writing to a bind-mounted ./data directory. docker exec web ls -ln /data shows the directory is owned by UID 0. Name two ways to fix this.
Show answer
Change the host directory's owner to UID 1000 with chown -R 1000:1000 ./data, or run the container as a user that can write to it, for example with --user "$(id -u):$(id -g)" when ./data belongs to your own user.