Log inSign up

Keep container data safe with volumes and bind mounts

Anything a container writes to its own filesystem disappears when the container is removed, so persistent data belongs in a named volume (-v appdata:/data) or a bind mount (-v /srv/app/data:/data). Named volumes are managed by Docker and suit databases; bind mounts suit config files you edit on the host. Back up a volume by archiving it from a short-lived container with tar.

Topic: Containers and PaaS · Tested on: Ubuntu 24.04 and 22.04, Debian 12 and 13, Rocky Linux 9, AlmaLinux 9 · Updated 2026-12-03

When should you pick a named volume or a bind mount?

Pick a named volume when the data belongs to the application and you rarely touch it by hand, such as database files, uploads or caches. Pick a bind mount when you want to edit files from the host, keep them in Git, or place them on a path you already back up. Both survive docker rm and image updates. A third type, tmpfs, lives in memory and vanishes on stop, which fits scratch space but never real data.

  • Named volume: created and tracked by Docker, stored under /var/lib/docker/volumes/NAME/_data, pre-filled with the image content on first use.
  • Bind mount: any host path, shown to the container exactly as it is on disk, hides whatever the image had at that path.
  • tmpfs: RAM only, useful for lock files and temporary uploads.

Docker itself must already be running; the install is covered in the Docker Engine tutorial.

How do you create and use a named volume?

Create it once, attach it with -v or the more explicit --mount, and Docker keeps it until you delete it yourself:

docker volume create pgdata
docker run -d --name pg --restart unless-stopped \
  -e POSTGRES_PASSWORD=change-me \
  --mount type=volume,source=pgdata,target=/var/lib/postgresql/data \
  postgres:17

docker volume ls
docker volume inspect pgdata

In a Compose file the same thing is a volumes: entry under the service plus a top level volumes: block that declares the name, as shown in the Compose guide. Compose prefixes the name with the project, so a volume called dbdata in the folder notes appears as notes_dbdata.

How do you set up a bind mount correctly?

Create the host directory first, then give an absolute path (or a path starting with ./ in Compose). With -v, a missing host path is silently created as an empty root-owned directory, which is a common source of confusing errors; --mount type=bind refuses a missing path instead, so mistakes surface at once:

mkdir -p /srv/web/html
echo "hello from the host" > /srv/web/html/index.html
docker run -d --name web -p 127.0.0.1:8088:80 \
  --mount type=bind,source=/srv/web/html,target=/usr/share/nginx/html,readonly \
  nginx:stable
curl http://127.0.0.1:8088

The readonly option (or :ro with the short syntax) stops the container from changing files it only needs to read, which is a good default for configuration and static content.

How do you back up a named volume?

Run a throwaway container that mounts the volume read-only and writes a compressed archive into a bind-mounted folder on the host. Stop the application first when the volume holds a database, or use the database dump tool instead, because copying live database files can capture a half-written state:

mkdir -p /srv/backups
docker stop pg
docker run --rm \
  -v pgdata:/source:ro \
  -v /srv/backups:/backup \
  alpine:3.20 \
  tar -czf /backup/pgdata-$(date +%F).tar.gz -C /source .
docker start pg
ls -lh /srv/backups

For PostgreSQL and MySQL a logical dump is often the cleaner choice, and it needs no downtime: docker exec pg pg_dump -U postgres mydb > /srv/backups/mydb.sql. Details are in the PostgreSQL backup guide and the MySQL backup guide. Archives on the same disk do not protect you from losing the server, so copy them elsewhere with a tool like rclone or restic, as described in the daily backups tutorial. That tutorial also covers the optional daily backup add-on for the whole server.

How do you restore a volume from an archive?

Create an empty volume and unpack the archive into it with the same kind of helper container, then start the application against it:

docker volume create pgdata_restored
docker run --rm \
  -v pgdata_restored:/target \
  -v /srv/backups:/backup:ro \
  alpine:3.20 \
  sh -c "cd /target && tar -xzf /backup/pgdata-2026-12-01.tar.gz"
docker run --rm -v pgdata_restored:/check alpine:3.20 ls -la /check

Restoring into a new name rather than over the old volume lets you compare both and switch back if the archive turns out to be older than you thought. Practise one restore on a test container before you rely on the process.

Why does the container get Permission denied on a bind mount?

The process inside the container runs as a numeric user id, and the host directory must allow that id to read or write. Names do not matter, only numbers. Find the id the image uses, then hand the directory to it:

docker run --rm nginx:stable id
docker exec -it app id
sudo chown -R 1000:1000 /srv/app/data
ls -ln /srv/app/data

Many images document a PUID/PGID variable or accept --user 1000:1000 so the container matches your host user. Avoid chmod 777; it hides the real problem and opens the files to every local account. Named volumes rarely show this issue because Docker copies the image ownership into a fresh volume.

What changes on Rocky Linux and AlmaLinux with SELinux?

With SELinux enforcing, which is the default on Rocky Linux 9 and AlmaLinux 9, a bind mount can fail with Permission denied even when the Unix permissions are correct. Add the :z suffix for a directory shared by several containers, or :Z for one used by a single container, and Docker relabels it:

docker run -d -v /srv/app/data:/data:Z myimage:1.0
ls -Z /srv/app/data
sudo ausearch -m avc -ts recent

Never use :Z on system paths such as /home or /etc, because relabelling them can lock out the host services. Background on contexts is in the SELinux basics guide.

How do you find and remove volumes you no longer need?

List volumes not attached to any container, check them, and delete deliberately:

docker volume ls -f dangling=true
docker system df -v
docker volume rm old_cache
docker volume prune

Since Docker Engine 23, docker volume prune only removes anonymous volumes by default; add --all to include unused named ones, and only after you are certain nothing needs them. Deleted volumes cannot be recovered. The wider picture of reclaiming disk is in the Docker cleanup guide.

Plans that fit this guide

Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.

All prices
PlanFits becauseSizeMonthlyOrder from
A2-4containers with a moderate database2 vCPU, 4 GB RAM, 100 GB NVMe$14.40/mo$43.20 for 3 months
F2-4fast NVMe for write heavy volumes2 vCPU, 4 GB RAM, 128 GB NVMe$14.40/mo$43.20 for 3 months

Questions about Docker volumes and bind mounts

Does docker rm delete my volume?

No. Removing a container keeps named volumes and bind-mounted folders. Only docker rm -v (for anonymous volumes), docker volume rm, prune or docker compose down -v delete volume data.

Can I move a volume to another server?

Yes. Archive it with the tar helper container, copy the file with scp or rsync, then restore it into a new volume on the target. This is the usual route when you move to a larger plan.

Where are named volumes stored on the disk?

Under /var/lib/docker/volumes on the server disk. Read the files there for inspection if you must, but make changes through a container to avoid ownership mistakes.

Should database files go on a bind mount or a named volume?

Either works. Named volumes avoid most permission trouble; bind mounts make the path obvious for host-level backup tools. Whichever you choose, back up with a dump tool for consistency.

Are volumes included in the server backup add-on?

The optional daily backups cover the whole server disk, and volumes live on that disk, so they are included. Database files copied while running may still need a dump for a clean restore.