Understanding Docker Volumes vs. Bind Mounts (With Real Examples)
Why do containers eat your data on updates? Bind mounts vs volumes explained, the path syntax that matters, and the :ro trick to protect your media.
The #1 Mistake Beginners Make in Docker
Docker containers are disposable by design. Whenever you update an image (docker compose pull && docker compose up -d), Docker deletes the old container and creates a brand-new one from scratch.
If your application wrote files inside the container’s internal filesystem without mapping storage outside, all your data vanishes the moment you update. Understanding how storage escapes the container is the single most important Docker concept for home server owners.
Bind Mounts vs. Named Volumes: Side-by-Side
Docker gives you two ways to persist data outside a container: Bind Mounts and Named Volumes.
| Feature | Bind Mount (HOST_PATH:CONTAINER_PATH) |
Named Volume (volume_name:CONTAINER_PATH) |
|---|---|---|
| Where files live on host | Anywhere you choose (/mnt/2TB/movies, ./config) |
Managed by Docker in /var/lib/docker/volumes/ |
Easy to browse with ls or SMB? |
Yes — plain normal folder | Harder (requires root to inspect /var/lib/docker) |
| Easy to map existing media disks? | Yes — point directly at /mnt/1TB_drive/ |
No (copies data into Docker’s internal folder) |
Permission headaches (PUID/PGID) |
Common if host UID doesn’t match container UID | Rare — Docker handles initial ownership automatically |
| Best used for | Media, photos, ./config, .env & YAML backups |
PostgreSQL, MySQL, Redis, internal app state |
Simple rule of thumb: use bind mounts for files you care about touching (movies, photos, app configs), and use named volumes for databases (PostgreSQL/MariaDB) that shouldn’t be poked at directly on disk anyway.
Recommended Host Folder Layout
Keeping paths predictable makes backups and troubleshooting effortless. On a typical Linux home server:
/opt/docker/ # Small OS SSD — fast configs & Compose files├── jellyfin/│ ├── docker-compose.yml│ └── config/ # Relative bind mount: ./config:/config└── immich/ ├── .env └── docker-compose.yml
/mnt/1TB_photos/ # Dedicated HDD — family photos/mnt/2TB_media/ # Dedicated HDD — movies & TV showsNotice the two styles of bind mounts:
- Relative bind mount (
./config:/config): starts with./, so it resolves inside the same folder as yourdocker-compose.yml(just like in our Docker Compose template). - Absolute bind mount (
/mnt/2TB_media/movies:/data/movies:ro): starts with/, pointing to a dedicated hard drive mounted in/etc/fstab.
Real-World Example Combining Both
Here is how a clean docker-compose.yml mixes a named volume (for PostgreSQL) with bind mounts (for app files and read-only media):
services: db: image: postgres:17-alpine container_name: app-db restart: unless-stopped environment: - POSTGRES_DB=myapp - POSTGRES_USER=myapp - POSTGRES_PASSWORD=strong-password-here volumes: - app_pgdata:/var/lib/postgresql/data # Named volume: Docker manages permissions
media-app: image: lscr.io/linuxserver/jellyfin:latest container_name: jellyfin restart: unless-stopped environment: - PUID=1000 - PGID=1000 - TZ=Europe/Madrid volumes: - ./config:/config # Relative bind mount on fast SSD - /mnt/2TB_media/movies:/data/movies:ro # Read-only (:ro) bind mount on HDD ports: - "8096:8096"
volumes: app_pgdata: # Declares the named volume at the bottom of the fileThe :ro flag at the end of /data/movies:ro stands for Read-Only. Even if a container bug or misclick triggers a delete inside Jellyfin’s UI, the Linux kernel refuses to let the container touch the files on your hard drive.
Essential Docker Volume Commands
Inspect what is actually stored on disk and find wasted space:
# 1. Check your host user's UID and GID before setting PUID/PGIDid
# 2. List all Docker named volumesdocker volume ls
# 3. See the exact host path and mount details of a volume or containerdocker volume inspect app_pgdatadocker inspect -f '{{ json .Mounts }}' jellyfin | python3 -m json.tool
# 4. Check disk space used by images, containers, and volumesdocker system dfWarning about cleanup: docker system prune alone does not remove named volumes unless you pass --volumes, and docker volume prune permanently deletes any volume not currently attached to a container. Never run --volumes if you have a stopped database stack you plan to start again later!
How to Back Up and Migrate a Named Volume
Because named volumes live inside /var/lib/docker/volumes/ (owned by root), copying them directly in a file browser feels awkward. The safest way to export a named volume cleanly is via a temporary Alpine container that archives it into a .tar.gz file on your host:
# Back up the named volume 'app_pgdata' into your current folderdocker run --rm \ -v app_pgdata:/source:ro \ -v $(pwd):/backup \ alpine tar czf /backup/app_pgdata-backup.tar.gz -C /source .Moving that archive to a new server is just the reverse command (tar xzf). For live SQL databases like PostgreSQL or MariaDB, exporting with pg_dump or mysqldump (see our rsync backup guide) is still better than copying raw volume files while the database is running.
Converting docker run -v flags cleanly
Found a longdocker run -v … -v … command in an app’s README? Paste it into our docker run to Compose converter and it turns every -v and –mount flag into tidy Compose YAML automatically.Troubleshooting Common Volume & Permission Errors
- Container logs show
Permission deniedwriting to./config: you created the folder usingsudo mkdir, so it is owned byroot:root, while the container runs asPUID=1000(PGID=1000). Check your UID withidand fix ownership on the host:sudo chown -R 1000:1000 ./config. - Database container (
postgres/mariadb) crashes in a boot loop on a bind mount: many database images run as internal UID999(postgres) or70and fail on strict host bind mounts orexFAT/NTFSdrives. Either switch the database path to a named volume (recommended) or verify permissions on anext4host folder. - Files created by Docker become undeletable over SMB or user SSH: a container running as
rootinside created files owned byroot:root(644) inside your bind mount. SetPUID/PGID(onlinuxserver.ioimages) oruser: "1000:1000"in your Compose file so host and container share the exact same owner. - Empty library inside the container even though files exist on the host: check that you didn’t map the wrong side of the colon (
HOST_PATH:CONTAINER_PATH— host is always on the left, container is always on the right), and make sure the underlying HDD in/mnt/actually mounted before Docker started (mountpoint -q /mnt/2TB_media).
Frequently Asked Questions
What happens to my files when I run docker compose down and docker compose up -d?
Anything stored inside the container's writable layer is wiped when the container is recreated. Anything mapped to a host folder (bind mount) or a Docker named volume persists safely across updates and recreations.
When should I use a named volume instead of a bind mount?
Use a named volume for internal databases (PostgreSQL, MariaDB, Redis) where Docker manages Linux permissions automatically and you export backups with pg_dump/mysqldump. Use a bind mount for media libraries, photos, Compose configs, and anything you want to browse directly on your host.
What does :ro at the end of a volume line do?
It mounts that folder inside the container as Read-Only (:ro). The app can read and stream files normally, but it is physically blocked by the Linux kernel from modifying or deleting anything inside that directory.
Do relative paths like ./config work everywhere?
Yes — ./config resolves relative to the folder containing your docker-compose.yml file. That keeps your entire app self-contained and easy to move or back up.