Docker volumes and persistent data: named volume vs bind mount
A Docker volume is storage managed by Docker that outlives any container that uses it.
- Updated Nov 27, 2026
- Markdown
Keep database data in a named volume that survives removing the container.
- Docker installed
To keep data after a container is removed, mount a Docker volume (or a host folder) into the container at the path where the app writes. A named volume such as -v pgdata:/var/lib/postgresql/data lives on after docker rm, and the next container that mounts it sees the same files.
How it works#
A container sees the image’s files (read-only) plus a thin writable layer on top. That layer is deleted with the container. A mount attaches storage from somewhere else into a path inside the container, and that path then survives the container.
There are two common kinds. A named volume is storage Docker creates and manages. On Linux it lives under /var/lib/docker/volumes/NAME/_data, and you refer to it only by name. A bind mount maps a folder you choose on the host into the container, so both sides see the same files.
| Named volume | Bind mount | |
|---|---|---|
| Created by | Docker, on first use | You, a host path |
| Best for | Data the app owns, such as a database | Config files, source code you edit on the host |
| Syntax | -v pgdata:/path | -v /host/path:/path |
| Content on first use | Filled from the image’s files at that path if the volume is empty | Hides the image’s files at that path |
The syntax decides which kind you get: if the left side starts with / (or ./ in Compose) it is a host path, otherwise it is a volume name. A mount also covers what the image had at that path, so mount only where you mean to.
A worked example, start to finish#
Start Postgres on a volume, create a table, destroy the container and look again.
docker run -d --name db -v pgdata:/var/lib/postgresql/data -e POSTGRES_PASSWORD=change-me postgres:16
docker exec db psql -U postgres -c "CREATE TABLE t (id int);"
docker rm -f db
docker run -d --name db -v pgdata:/var/lib/postgresql/data -e POSTGRES_PASSWORD=change-me postgres:16
docker exec db psql -U postgres -c "\dt"Wait a few seconds after the second docker run so the server is ready. The last command lists the table t. The container was new, the data was not: it sat in the volume the whole time. Run the same steps without the -v flag and the table is gone.
Step 1: Start a container with a volume#
docker run -d --name db -v pgdata:/var/lib/postgresql/data -e POSTGRES_PASSWORD=change-me postgres:16-v pgdata:/var/lib/postgresql/data mounts the volume pgdata at Postgres’s data folder. Docker creates the volume the first time.
What you should see: a container ID, and docker ps shows db as Up.
Step 2: Check the volume#
docker volume ls
docker volume inspect pgdataWhat you should see: pgdata in the list with driver local, and JSON with a Mountpoint on the host and "Name": "pgdata".
Step 3: Prove the data survives#
docker rm -f db
docker run -d --name db -v pgdata:/var/lib/postgresql/data -e POSTGRES_PASSWORD=change-me postgres:16-f force-removes the container even while it runs. The volume is untouched. The new container starts with the old data, so Postgres starts without initialising a new database.
What you should see: docker logs db mentions that the database directory already contains a database and skips initialisation.
Step 4: Back it up#
docker run --rm -v pgdata:/data -v "$PWD":/backup alpine tar czf /backup/pgdata.tgz -C /data .This starts a throwaway Alpine container with the volume at /data and your current folder at /backup, archives the volume into pgdata.tgz, and removes itself (--rm). For a running database, a dump with pg_dump (see Back up a Postgres database in Docker) is safer than copying files, because files copied mid-write can be inconsistent. Stop the database first if you must copy files.
What you should see: a pgdata.tgz file in your current folder.
Check it worked#
docker volume ls lists pgdata before and after docker rm -f db. After the second docker run, docker volume ls still shows one pgdata, not two. ls -lh pgdata.tgz shows a non-empty archive.
Edge cases and gotchas#
- Anonymous volumes pile up.
-v /var/lib/postgresql/data(no name) creates a volume with a random ID each time. They keep data but are hard to find, anddocker volume lsfills with hash names. Always name volumes you care about. - A bind mount hides the image’s files. Mounting an empty host folder over
/appmakes the container see an empty/app. Only an empty named volume is filled from the image on first use. - Permissions on bind mounts. The container’s user is a numeric ID, and it must be allowed to write the host folder. Fix with
chownon the host, or run the container as a user ID that owns the folder. - Docker may create a missing host path as root. With
-v /host/new:/data, a missing folder is created owned by root. The--mountform fails instead, which is stricter. - Initial settings apply only once. Variables such as
POSTGRES_PASSWORDare read only when the volume is empty. A reused volume keeps its old data and old password. - A volume in use cannot be removed.
docker volume rm pgdatafails while a container (even a stopped one) uses it. Remove the container first. - Clean-ups can delete volumes.
docker volume prune(see Free disk space with docker prune) removes volumes no container uses. A volume whose container you just removed counts as unused.
Troubleshooting#
| Symptom | Likely cause | Fix |
|---|---|---|
| Data gone after recreating the container | No volume, or a different volume name or path | Check docker inspect db under Mounts, and use the same name |
volume is in use | A container still references the volume | docker rm that container, then remove the volume |
permission denied writing to a mounted folder | UID of the container user cannot write the host folder | chown the folder, or run with a matching --user |
| Mounted folder looks empty inside the container | The mount hides the image’s content, or the host path is wrong | Check the host path and the target path |
invalid mount config for type "bind": bind source path does not exist | --mount with a missing host folder | Create the folder first |
| Backup archive is tiny or empty | The wrong volume name, so Docker made a new empty volume | Check docker volume ls and the name in -v |
docker volume ls shows many hash-named volumes | Anonymous volumes created by -v /path or by the image | Name the volumes you care about, and remove old ones with care |
| Compose data seems missing after you moved the project | Compose prefixes the volume with the project name, so a new folder name means a new empty volume | Keep the project name, or set name: on the volume |
Restore from pgdata.tgz gives an empty folder | The archive path or -C option was wrong | List it with tar tzf pgdata.tgz before restoring |
Real-world scenario#
A small team runs a wiki in Docker. The first version stores pages inside the container, and an upgrade (docker rm plus a new image) wipes every page on a Friday afternoon. The fix is two lines: a named volume on the wiki’s data folder, and the same volume name in the new docker run.
The following week they add a nightly backup. A cron job runs the Alpine tar command from step 4 against the volume, then copies the archive to another machine. They also read Backups and the 3-2-1 rule and test one restore. The next upgrade is boring: stop, remove the container, pull the new image, start it on the same volume.
Quick reference#
| Task | Command |
|---|---|
| Create a volume | docker volume create pgdata |
| List and inspect | docker volume ls, docker volume inspect pgdata |
| Mount a named volume | -v pgdata:/var/lib/postgresql/data |
| Mount a host folder | -v /srv/app/config:/etc/app:ro |
| Back up a volume | docker run --rm -v pgdata:/data -v "$PWD":/backup alpine tar czf /backup/pgdata.tgz -C /data . |
| Remove a volume (data loss) | docker volume rm pgdata |
Security and safety notes#
docker volume rm,docker volume pruneanddocker compose down -vdelete data permanently. Back up first.- Do not copy the files of a running database as your only backup. Use
pg_dumpor stop it first. - Keep backups on another machine as well as on the same disk. See Backups and the 3-2-1 rule.
Related lessons#
- Docker Compose basics: declare volumes in
compose.yaml - Back up a Postgres database in Docker: a safer dump than copying files
- Test a backup by restoring it: prove the archive works
- Free disk space with docker prune, safely: how prune treats volumes
Next#
Turn your own app into an image with Write your first Dockerfile.
Try it: Prove a volume keeps data
Start Postgres with a named volume, inspect it, remove the container and start it again.
Want a real machine? Free browser labs: Killercoda Play with Docker
Check what you learned
Frequently asked questions
Volume or bind mount?
Use a named volume for data an app owns, such as a database. Use a bind mount when you need to edit files on the host, such as config or source code.
Where is a named volume stored on the server?
Under /var/lib/docker/volumes/NAME/_data on a Linux host. Use docker volume inspect NAME to see the exact path. Treat it as Docker's, and read or copy it with a container or backup command instead of editing it by hand.
Why does my container get permission denied on a bind mount?
The user inside the container is a numeric user ID, and it must be allowed to read and write the host folder. Match ownership with chown, or run the container as a user that owns the folder.
Sources
Keep going: guides and tutorials
- Deploy a Docker Compose App Self-Hosted: Web, Redis and Rollback Deploy a Docker Compose app on your own server with Levelrail: a web service and Redis, a bad release that does not take traffic, and a compose rollback.
- Docker Container Restarting in a Loop: 5 Causes, Fixed Five real reasons a Docker container keeps restarting, each reproduced with its exit code, the command that finds it and the fix. Then the same crash on Levelrail.
- Deploy an Express App to a VPS With Docker and Health Checks Deploy a Node.js Express API to your own server with no Dockerfile. Covers the port rule, a health route, secrets, deploy failure codes and rollback.