Docker volumes vs bind mounts: store container data safely
Learn the difference between Docker named volumes and bind mounts, how to create, inspect and back up volumes, fix file permissions on bind mounts and find where the data lives on disk.
- Intermediate
- Linux VPS
- 5 min read
Containers are disposable, but your database files, uploads and configuration are not. Docker gives you two main ways to keep data outside a container: named volumes and bind mounts. This guide explains how each works, when to use which, and how to back them up so a docker rm never costs you data.
Before you start
- A Linux VPS with Docker Engine and Compose installed. See Install Docker and Docker Compose.
- A user that can run
dockercommands, plussudofor reading files under/var/lib/docker. - Enough free disk for a backup copy of your largest volume.
The difference in one paragraph
A named volume is storage that Docker creates and manages. You refer to it by name, and Docker decides where it lives on disk. A bind mount maps a folder you choose on the host, such as /srv/app/config, straight into the container. Both survive container removal. The difference is who manages the path and the permissions.
Use named volumes for:
- Database data directories (PostgreSQL, MariaDB, Redis).
- Any data the app owns and you rarely edit by hand.
Use bind mounts for:
- Configuration files you edit on the host, such as an
nginx.confor aCaddyfile. - Source code during development.
- Folders another host tool also needs, such as a backup agent.
Work with named volumes
Create a volume and attach it to a container:
docker volume create pgdata
docker run -d --name db -e POSTGRES_PASSWORD=change-me \
-v pgdata:/var/lib/postgresql/data postgres:16If the volume does not exist, docker run creates it for you. List and inspect volumes:
docker volume ls
docker volume inspect pgdataThe Mountpoint field shows where the files are, usually /var/lib/docker/volumes/pgdata/_data. Read them with sudo, but do not edit them while the container runs.
In Compose, declare the volume at the bottom of the file:
services:
db:
image: postgres:16
environment:
POSTGRES_PASSWORD: change-me
volumes:
- pgdata:/var/lib/postgresql/data
volumes:
pgdata:Compose prefixes the name with the project name, so on disk it becomes something like myproject_pgdata.
Work with bind mounts
A bind mount uses an absolute host path, or a path relative to the Compose file:
services:
web:
image: nginx:stable
volumes:
- ./site:/usr/share/nginx/html:ro
- ./nginx.conf:/etc/nginx/conf.d/default.conf:roThe :ro suffix makes the mount read-only inside the container, which is a good default for configuration.
With docker run, use the longer --mount syntax if you want an error when the host path is missing, instead of Docker silently creating an empty folder:
docker run -d --name web \
--mount type=bind,src=/srv/site,dst=/usr/share/nginx/html,readonly \
nginx:stableFix permissions on bind mounts
Docker does not translate file ownership. Inside the container, files keep the same numeric user and group IDs they have on the host. If the app runs as UID 1000 but the folder belongs to root, writes fail with Permission denied.
Find which user the container runs as:
docker exec web idThen give that UID ownership of the host folder:
sudo chown -R 1000:1000 /srv/app/dataSome images let you set the user with environment variables such as PUID and PGID. Check the image's documentation.
Do not chmod -R 777 a data folder to make an error go away. It lets every user and process on the server read and change the data.
Back up and restore a named volume
For a database, the safest backup is the database's own dump tool, for example pg_dump or mysqldump. For other volumes, archive the files with a throwaway container.
Stop the app first so files are consistent:
docker stop app
docker run --rm -v app-data:/data:ro -v "$PWD":/backup debian:12 \
tar czf /backup/app-data.tar.gz -C /data .
docker start appRestore into a new or empty volume:
docker volume create app-data-restored
docker run --rm -v app-data-restored:/data -v "$PWD":/backup debian:12 \
tar xzf /backup/app-data.tar.gz -C /dataCopy the archive off the server. A backup on the same disk does not protect you from a disk or server loss. For schedules and off-site copies, see Back up and restore Docker volumes.
Remove volumes safely
Volumes are not deleted when you remove a container. List the ones no container uses:
docker volume ls -f dangling=truedocker volume rm and docker compose down -v delete the data permanently. Check the name twice and make sure you have a backup.
Check it works
Write a file, remove the container, start a new one and read the file back:
docker run --rm -v testvol:/data debian:12 sh -c 'echo hello > /data/test.txt'
docker run --rm -v testvol:/data debian:12 cat /data/test.txt
docker volume rm testvolThe second command should print hello, which proves the data outlived the first container.
Troubleshooting
- Data disappeared after recreating a container: the path inside the container did not match the image's data directory, so data was written to the container layer. Check the image documentation for the correct path.
- A bind-mounted file shows as an empty folder: the host path did not exist when the container started, so Docker created a directory. Remove it, create the file, then restart.
- `Permission denied` writing to a bind mount: the container user's UID does not own the host folder. Use
chownas shown above. - Volume is large and the disk is full: find big volumes with
docker system df -v. Free disk space with docker system prune explains safe cleanup.
Next steps
- Automate off-site copies: Back up and restore Docker volumes.
- Reclaim space safely: Free disk space with docker system prune.
- Need more disk for your data? Compare Linux VPS plans.
Was this guide helpful?
Still stuck?
Ask RunRacks AI for a quick answer, or open a ticket and our team will take a look at your server.
Related guides
Docker & DevOps
Self-host Jellyfin with Docker and hardware transcoding
Install Jellyfin in Docker on a Linux VPS, mount your media library, publish it over HTTPS and switch on hardware transcoding with VAAPI or Intel Quick Sync.
- Intermediate
- Linux VPS
- 6 min read
Docker & DevOps
Deploy a Self-Hosted App with Docker Compose from Scratch
Write a compose.yaml, keep your secrets in .env, start the stack with docker compose up -d, then verify it and update it later without losing data.
- Beginner
- Linux VPS
- 7 min read
Docker & DevOps
Manage secrets and environment variables in Docker Compose
Keep API keys and database passwords out of your compose file using .env files, environment keys and Docker secrets, then confirm containers read them correctly.
- Intermediate
- Linux VPS
- 7 min read