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.

DevOps & Cloud Stop 1 of 2 By UniqBlog Desk 1 December 2026 Updated 1 December 2026 5 min read

Soft 3D illustration: a grey shipping container with a drawer pulled out, holding a green drum, a short wide drum and a slim canister, above a thin green route line

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: pgdata becomes voldemo_pgdata.
  • docker compose down keeps volumes; docker compose down -v deletes them, except volumes marked external.
  • 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 volumeBind mounttmpfs
Where data livesDocker's storage area, managed by DockerA host path you chooseHost memory
Survives container removalYesYes (it is your folder)No
Created byDocker or ComposeYouDocker, at container start
Typical useDatabase files, uploads, caches you keepSource code in dev, config files, certificatesTemp files, sockets, short-lived scratch data
Easy to back upYes, with a helper containerYes, with normal host toolsNothing to back up
Portable across hostsYes, the path is not in your fileNo, the path is host-specificn/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/postgresql line) 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. Set name: 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 -v as "delete my data". We checked: docker compose down left voldemo_pgdata in place, down -v removed it, and down -v on 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:

  1. Know which volumes exist: docker volume ls --filter label=com.docker.compose.project=<name>.
  2. Back up on a schedule, not before an upgrade only.
  3. Copy archives off the host; a backup on the same disk is not a backup.
  4. Test a restore into a new volume, as above, at least once.
  5. Keep secrets out of bind-mounted folders that you also commit to git.

Sources

UniqBlog Desk · Editors

The editors of UniqBlog. Tested tutorials and tech news for working developers.

Further along the line