Home DockerDocker Volumes vs Bind Mounts: A Data Persistence Guide

Docker Volumes vs Bind Mounts: A Data Persistence Guide

by Knowledge Base Linux
Docker Volumes vs Bind Mounts — Docker Volumes vs Bind Mounts: A Data Persistence Guide

Every Docker beginner hits the same surprise sooner or later: a database container is removed, and with it every record it ever stored. That happens because containers are ephemeral by design — the writable layer of a container disappears the moment the container is deleted. Docker volumes are Docker’s answer to this problem: they are persistent storage areas managed by the Docker daemon that live outside the container lifecycle. Alongside bind mounts, they form the two main ways to persist or share data with containers. In this guide you will learn how Docker volumes work, when to prefer them over bind mounts, and the practical commands for backup, restore, and permission management.

Backing up Docker volumes with a tar archive illustration

Prerequisites – Docker Volumes vs Bind Mounts

  • A Linux server or workstation with Docker Engine installed. If you have not installed it yet, see how to install Docker on Ubuntu 22.04.
  • Basic familiarity with docker run and docker-compose / docker compose.
  • Sudo or root access on the host.

What Are Docker Volumes? – Docker Volumes vs Bind Mounts

A volume is a directory (or file) that lives outside the container’s writable layer, managed by Docker itself under /var/lib/docker/volumes/ on the host. When you mount a volume into a container, the container reads and writes data there, but the data survives container restarts, updates, and even container removal. Docker volumes can be shared between multiple containers and are portable — you can back them up, migrate them, and attach them to a new container on another host.

There are two volume flavors: named volumes, which Docker manages by name, and anonymous volumes, which get a random hex name and are harder to track. In practice you should always use named volumes; anonymous volumes tend to pile up and confuse cleanup.

Creating, Listing, and Inspecting Named Volumes

# Create a named volume
docker volume create app-data

# List all volumes
docker volume ls

# Inspect a volume (shows mountpoint on the host)
docker volume inspect app-data

# Remove a single volume
docker volume rm app-data

# Remove all volumes not used by at least one container (careful!)
docker volume prune

The docker volume inspect output shows the host path under Mountpoint. You almost never need to touch that directory directly — treat it as Docker’s internal storage — but knowing where it is helps when debugging disk usage.

Mounting Docker Volumes in Containers

The classic syntax is the short -v flag:

# Mount the named volume app-data at /data inside the container
docker run -d --name web -v app-data:/data nginx:latest

# The volume is created automatically if it does not exist
docker run -d --name db -v db-data:/var/lib/mysql mysql:8.4

The newer, more explicit --mount syntax is preferred for scripts and documentation because each field is named:

docker run -d --name web \
  --mount source=app-data,target=/data \
  nginx:latest

If you forget which container uses which volume, docker inspect shows the mounts:

docker inspect web --format '{{ json .Mounts }}' | python3 -m json.tool

Volumes in Docker Compose

Compose makes Docker volumes declarative. Define named volumes at the top level and reference them in services:

version: "3.8"

services:
  db:
    image: mysql:8.4
    environment:
      MYSQL_ROOT_PASSWORD: example-please-change
    volumes:
      - db-data:/var/lib/mysql

  app:
    build: .
    volumes:
      - app-data:/usr/src/app/uploads

volumes:
  db-data:
  app-data:

Run docker compose up -d and Compose creates the volumes automatically. They persist across docker compose down; only docker compose down -v removes them, which is exactly the footgun that deletes production data. Always double-check before adding -v to a down command.

Bind Mounts and When to Use Them

A bind mount maps a specific file or directory from the host filesystem into the container, rather than a Docker-managed volume:

# Bind mount a host directory into the container
docker run -d --name web \
  --mount type=bind,source=/home/deploy/site,target=/usr/share/nginx/html \
  nginx:latest

Bind mounts shine in development workflows: mount your source code directory into the container so edits on the host appear instantly inside the container without rebuilding the image. They are also useful when the container needs access to host configuration files (for example, mounting /etc/timezone or a custom nginx.conf).

Downsides: bind mounts tie the container to a specific host path, so they are not portable, and a typo in the host path can mount an empty directory over your data. They also behave differently on Docker Desktop (macOS/Windows) where file sharing crosses a VM boundary, which can make I/O noticeably slower. For production data that should survive independently of the host layout, prefer named Docker volumes; for live code and config during development, bind mounts are the pragmatic choice.

Read-Only Mounts and tmpfs Mounts

Any volume or bind mount can be mounted read-only, which is a simple hardening step — a compromised process cannot tamper with mounted config or secrets:

# Read-only volume mount
docker run -d --name web -v app-config:/etc/app:ro nginx:latest

# Read-only bind mount with the long syntax
docker run -d --name web \
  --mount type=bind,source=/home/deploy/nginx.conf,target=/etc/nginx/nginx.conf,readonly \
  nginx:latest

For data that should never touch disk at all — session caches, temporary uploads — use a tmpfs mount, which lives in host memory only and vanishes when the container stops:

docker run -d --name cache --tmpfs /run/app-cache redis:7-alpine

# Limit the tmpfs size with the long syntax
docker run -d --name cache \
  --mount type=tmpfs,destination=/run/app-cache,tmpfs-size=100m \
  redis:7-alpine

Backing Up and Restoring Docker Volumes

Docker volumes live under Docker’s storage directory, so back them up with the tar-in-a-temporary-container trick rather than copying host paths directly. To back up the volume db-data to a tar file in the current directory:

docker run --rm \
  -v db-data:/volume \
  -v "$(pwd)":/backup \
  busybox tar cvf /backup/db-data-backup.tar -C /volume .

This launches a throwaway busybox container, mounts the volume at /volume, and archives its contents. To restore into a (possibly new) volume:

# Create the target volume first if it does not exist
docker volume create db-data-restored

docker run --rm \
  -v db-data-restored:/volume \
  -v "$(pwd)":/backup \
  busybox tar xvf /backup/db-data-backup.tar -C /volume

Schedule the backup command in a cron job and copy the tar files off the host (to S3, an NFS share, or another backup target) and you have a workable volume backup strategy. For databases, a logical dump (mysqldump, pg_dump) taken from a running container is usually safer than a raw filesystem backup, because it guarantees a consistent snapshot.

Permission Pitfalls: UID, GID, and SELinux Labels

The most common volume headache is permissions. Files in a volume are owned by numeric UID/GID, and the container process may run as a different user than the one that created the files. Symptoms: the app logs “permission denied” writing to a mounted volume.

# Find the UID the container process runs as
docker exec web id

# Fix ownership inside the container (example: www-data is uid 33)
docker exec web chown -R www-data:www-data /data

# Or set ownership at run time with --user
docker run -d --name web --user 1000:1000 -v app-data:/data nginx:latest

On SELinux-enforcing hosts (RHEL, AlmaLinux, Fedora), add the :z or :Z suffix so SELinux relabels the mount for container access — :z for volumes shared between containers, :Z for a private mount:

docker run -d --name web -v app-data:/data:Z nginx:latest

Without the label you will see mysterious permission errors even when the Unix permissions look correct. If you build multi-stage images and want smaller, cleaner production images, pair good volume practices with Docker multi-stage builds for Node and Python to keep secrets out of image layers.

Docker Volumes vs Bind Mounts vs tmpfs: Quick Comparison

Aspect Named volumes Bind mounts tmpfs
Managed by Docker daemon You (host path) Host memory
Portability High — name travels with Compose Low — tied to host path None — ephemeral
Best for Production data: databases, uploads Dev code, host config files Caches, temp files
Backup Tar-container trick Normal file backup Not needed
Performance Native filesystem speed Slower on Docker Desktop RAM speed

Troubleshooting

  • Container starts but data is missing: you probably mounted a fresh empty volume over a directory that already had data in the image. Named volumes are initialized with the image’s content only on first creation; if the volume already existed, the old (empty) content wins. Inspect with docker volume inspect and, if safe, remove and recreate the volume.
  • “permission denied” writing to the mount: check the container UID with docker exec <name> id and fix ownership, or add :Z on SELinux hosts.
  • docker compose down -v deleted production data: this is by design — -v removes named volumes declared in the Compose file. Restore from your tar backup, and consider removing the -v habit from your runbooks.
  • Disk full under /var/lib/docker: list volume usage with docker system df -v, then prune unused volumes with docker volume prune after confirming nothing important is unattached.
  • Mount path behaves differently on Docker Desktop: bind mounts on macOS/Windows cross a VM boundary — for hot-reload dev work this is acceptable, but keep databases on named volumes for speed.

Conclusion

Docker volumes are the default, portable way to persist container data: create them by name, mount them with -v or --mount, declare them in Compose, and back them up with the tar-container trick. Reach for bind mounts when you need live access to host files during development, mount either read-only when the container should not write, and use tmpfs for data that must never hit disk. Get the permissions right — matching UID/GID and SELinux labels on enforcing hosts — and your container data will survive every restart, rebuild, and redeploy. For the full storage reference, see the official Docker volumes documentation.

You may also like

KbLinux

Typically replies within a day

Hello, Welcome to the site. Please click below button for chatting me through Telegram.