Docker Compose volumes: bind mounts, tmpfs and backups
Docker Compose volumes explained with a tested compose file: when to use named volumes, bind mounts or tmpfs, how to name them, and how to back up and restore one.
Use a named volume for data the container owns (a database, uploads), a bind mount for files you edit on the host (source code, config), and tmpfs for scratch data that should vanish when the container stops. To back up a named volume, mount it read-only into a throwaway container and tar it to a host folder; to restore, untar into a new volume.
Everything below ran on Docker Engine 29.9.3 with Docker Compose v5.7.0. Current releases as of December 2026 are Engine 29.10.1 and Compose v5.8.1; nothing here depends on the difference.
Key takeaways
- Named volumes outlive containers and are the right home for database data.
- Compose prefixes volume names with the project name:
pgdatabecomesvoldemo_pgdata. docker compose downkeeps volumes;docker compose down -vdeletes them, except volumes markedexternal.- A file-level backup of a database volume is only safe with the database stopped. A logical dump (
pg_dump) works while it runs.
How do volumes differ from bind mounts and tmpfs?
| Named volume | Bind mount | tmpfs | |
|---|---|---|---|
| Where data lives | Docker's storage area, managed by Docker | A host path you choose | Host memory |
| Survives container removal | Yes | Yes (it is your folder) | No |
| Created by | Docker or Compose | You | Docker, at container start |
| Typical use | Database files, uploads, caches you keep | Source code in dev, config files, certificates | Temp files, sockets, short-lived scratch data |
| Easy to back up | Yes, with a helper container | Yes, with normal host tools | Nothing to back up |
| Portable across hosts | Yes, the path is not in your file | No, the path is host-specific | n/a |
Docker's volumes guide puts the trade-off plainly: volumes "are easier to back up or migrate than bind mounts", while bind mounts are for when you "need to access files or directories from both containers and the host". tmpfs mounts live in memory, are Linux-only and cannot be shared between containers. They are not a vault either: the docs warn that tmpfs data may be written to swap.
What does a compose file using all three look like?
This file runs PostgreSQL 18 with each mount type. It uses the long syntax, which spells out the type instead of guessing it from the string:
# compose.yaml
name: voldemo
services:
db:
image: postgres:18
environment:
POSTGRES_PASSWORD: example-only
volumes:
# named volume: Docker manages where the bytes live
- type: volume
source: pgdata
target: /var/lib/postgresql
# bind mount: a host folder you edit, mounted read-only
- type: bind
source: ./initdb
target: /docker-entrypoint-initdb.d
read_only: true
# tmpfs: in-memory scratch space, gone when the container stops
- type: tmpfs
target: /tmp
tmpfs:
size: 67108864 # 64 MiB
ports:
- "127.0.0.1:55433:5432"
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 2s
retries: 15
volumes:
pgdata:
labels:
dev.uniqblog.purpose: "demo database"
initdb/01-notes.sql creates a table and inserts one row. The mount point matters: the postgres:18 image declares its volume at /var/lib/postgresql (check with docker image inspect postgres:18), not the older /var/lib/postgresql/data.
docker compose up -d --wait
docker volume ls --filter label=com.docker.compose.project=voldemo
# DRIVER VOLUME NAME
# local voldemo_pgdata
docker compose exec db df -h /tmp | tail -1
# tmpfs 64M 0 64M 0% /tmp
How should you name and use volumes in compose files?
- Use named volumes, not anonymous ones. An anonymous volume (a bare
- /var/lib/postgresqlline) gets a random hash name and is easy to lose. - Let the project prefix work for you. Compose names the volume
{project}_{key}, so two checkouts with different project names do not share data. Setname:at the top of the file so the prefix does not depend on the folder name. - Use
name:on a volume only on purpose. The Compose volumes reference says that name "is used as is and is not scoped with the stack name". Good for a volume shared on purpose, risky by accident. - Mark long-lived data
external: true. Compose then never creates or deletes it, and errors if it is missing. - Label volumes so
docker volume ls --filter label=…finds them in scripts. - Mount config read-only (
read_only: true) so a container cannot rewrite your host files. - Treat
down -vas "delete my data". We checked:docker compose downleftvoldemo_pgdatain place,down -vremoved it, anddown -von a project using an external volume left that volume alone.
How do you back up a Docker volume?
For a database, stop the service so the files on disk are consistent, then tar the volume from a throwaway Alpine container:
docker compose stop db
mkdir -p backups
docker run --rm \
-v voldemo_pgdata:/source:ro \
-v "$PWD/backups":/backup \
alpine:3 \
tar czf /backup/pgdata-$(date +%F).tar.gz -C /source .
docker compose start db
ls -lh backups
# -rw-r--r-- 1 root root 4.7M Dec 1 14:48 pgdata-2026-12-01.tar.gz
Two details from the run. :ro stops the backup container from changing the source. The archive is owned by root because the container ran as root; chown it afterwards, or add --user "$(id -u):$(id -g)" when the volume's files are readable by your user.
Docker's own example uses --volumes-from with an ubuntu image. The idea is the same; naming the volume directly is clearer in scripts.
How do you restore or migrate a volume to a new setup?
On the new machine, or under a new project name, create an empty volume and unpack the archive into it:
docker volume create restored_pgdata
docker run --rm \
-v restored_pgdata:/target \
-v "$PWD/backups":/backup:ro \
alpine:3 \
tar xzf /backup/pgdata-2026-12-01.tar.gz -C /target
Then point a compose file at it as an external volume:
services:
db:
image: postgres:18
volumes:
- pgdata:/var/lib/postgresql
volumes:
pgdata:
external: true
name: restored_pgdata
Before the backup we added a second row to the original database, after the snapshot was taken. The restored copy had exactly one row, the one that existed at backup time:
id | body
----+---------------------------
1 | written before the backup
Use the same major PostgreSQL version on both sides. Data files are not compatible across major versions; moving from 18 to 19 needs pg_upgrade or a dump and restore.
Should you back up files or a database dump?
For databases, prefer a logical dump in addition to, or instead of, the file copy. It runs while the database is up and restores into any newer major version:
docker compose exec -T db pg_dump -U postgres --format=custom postgres > backups/postgres.dump
# restore later with pg_restore into a fresh container
Use this checklist for any volume holding data you care about:
- Know which volumes exist:
docker volume ls --filter label=com.docker.compose.project=<name>. - Back up on a schedule, not before an upgrade only.
- Copy archives off the host; a backup on the same disk is not a backup.
- Test a restore into a new volume, as above, at least once.
- Keep secrets out of bind-mounted folders that you also commit to git.
Sources
Transfer to
UniqBlog Desk · Editors
The editors of UniqBlog. Tested tutorials and tech news for working developers.
Further along the line

Kubernetes vs Docker: which one do you actually need?
Kubernetes vs Docker is not either-or: Docker builds and runs containers, Kubernetes schedules them across machines. Here is when each layer earns its cost.

PostgreSQL vs MySQL in 2026: which database should you pick?
PostgreSQL vs MySQL compared on current versions: JSON, CTEs, window functions, extensions, replication, licensing and cost, with SQL we ran on both.

Secrets management across environments: a practical ladder
A secrets management ladder for dev, staging and prod: from .env files to SOPS and age, cloud secret managers, Vault or OpenBao, and External Secrets Operator.

Git rebase vs merge: when to use each, with a demo
Git rebase vs merge with a reproducible demo: how each shapes history, when to rebase, the golden rule, safe force-pushes, and git pull vs fetch.