Docker volumes vs bind mounts: store container data safely

Learn the difference between Docker named volumes and bind mounts, how to create, inspect and back up volumes, fix file permissions on bind mounts and find where the data lives on disk.

  • Intermediate
  • Linux VPS
  • 5 min read

By RunRacks Team · Published

Containers are disposable, but your database files, uploads and configuration are not. Docker gives you two main ways to keep data outside a container: named volumes and bind mounts. This guide explains how each works, when to use which, and how to back them up so a docker rm never costs you data.

Before you start

  • A Linux VPS with Docker Engine and Compose installed. See Install Docker and Docker Compose.
  • A user that can run docker commands, plus sudo for reading files under /var/lib/docker.
  • Enough free disk for a backup copy of your largest volume.

The difference in one paragraph

A named volume is storage that Docker creates and manages. You refer to it by name, and Docker decides where it lives on disk. A bind mount maps a folder you choose on the host, such as /srv/app/config, straight into the container. Both survive container removal. The difference is who manages the path and the permissions.

Use named volumes for:

  • Database data directories (PostgreSQL, MariaDB, Redis).
  • Any data the app owns and you rarely edit by hand.

Use bind mounts for:

  • Configuration files you edit on the host, such as an nginx.conf or a Caddyfile.
  • Source code during development.
  • Folders another host tool also needs, such as a backup agent.

Work with named volumes

Create a volume and attach it to a container:

Terminal
docker volume create pgdata
docker run -d --name db -e POSTGRES_PASSWORD=change-me \
  -v pgdata:/var/lib/postgresql/data postgres:16

If the volume does not exist, docker run creates it for you. List and inspect volumes:

Terminal
docker volume ls
docker volume inspect pgdata

The Mountpoint field shows where the files are, usually /var/lib/docker/volumes/pgdata/_data. Read them with sudo, but do not edit them while the container runs.

In Compose, declare the volume at the bottom of the file:

YAML
services:
  db:
    image: postgres:16
    environment:
      POSTGRES_PASSWORD: change-me
    volumes:
      - pgdata:/var/lib/postgresql/data
volumes:
  pgdata:

Compose prefixes the name with the project name, so on disk it becomes something like myproject_pgdata.

Work with bind mounts

A bind mount uses an absolute host path, or a path relative to the Compose file:

YAML
services:
  web:
    image: nginx:stable
    volumes:
      - ./site:/usr/share/nginx/html:ro
      - ./nginx.conf:/etc/nginx/conf.d/default.conf:ro

The :ro suffix makes the mount read-only inside the container, which is a good default for configuration.

With docker run, use the longer --mount syntax if you want an error when the host path is missing, instead of Docker silently creating an empty folder:

Terminal
docker run -d --name web \
  --mount type=bind,src=/srv/site,dst=/usr/share/nginx/html,readonly \
  nginx:stable

Fix permissions on bind mounts

Docker does not translate file ownership. Inside the container, files keep the same numeric user and group IDs they have on the host. If the app runs as UID 1000 but the folder belongs to root, writes fail with Permission denied.

Find which user the container runs as:

Terminal
docker exec web id

Then give that UID ownership of the host folder:

Terminal
sudo chown -R 1000:1000 /srv/app/data

Some images let you set the user with environment variables such as PUID and PGID. Check the image's documentation.

Back up and restore a named volume

For a database, the safest backup is the database's own dump tool, for example pg_dump or mysqldump. For other volumes, archive the files with a throwaway container.

Stop the app first so files are consistent:

Terminal
docker stop app
docker run --rm -v app-data:/data:ro -v "$PWD":/backup debian:12 \
  tar czf /backup/app-data.tar.gz -C /data .
docker start app

Restore into a new or empty volume:

Terminal
docker volume create app-data-restored
docker run --rm -v app-data-restored:/data -v "$PWD":/backup debian:12 \
  tar xzf /backup/app-data.tar.gz -C /data

Copy the archive off the server. A backup on the same disk does not protect you from a disk or server loss. For schedules and off-site copies, see Back up and restore Docker volumes.

Remove volumes safely

Volumes are not deleted when you remove a container. List the ones no container uses:

Terminal
docker volume ls -f dangling=true

Check it works

Write a file, remove the container, start a new one and read the file back:

Terminal
docker run --rm -v testvol:/data debian:12 sh -c 'echo hello > /data/test.txt'
docker run --rm -v testvol:/data debian:12 cat /data/test.txt
docker volume rm testvol

The second command should print hello, which proves the data outlived the first container.

Troubleshooting

  • Data disappeared after recreating a container: the path inside the container did not match the image's data directory, so data was written to the container layer. Check the image documentation for the correct path.
  • A bind-mounted file shows as an empty folder: the host path did not exist when the container started, so Docker created a directory. Remove it, create the file, then restart.
  • `Permission denied` writing to a bind mount: the container user's UID does not own the host folder. Use chown as shown above.
  • Volume is large and the disk is full: find big volumes with docker system df -v. Free disk space with docker system prune explains safe cleanup.

Next steps

Was this guide helpful?

Still stuck?

Ask RunRacks AI for a quick answer, or open a ticket and our team will take a look at your server.