Home DockerDocker Volumes Explained: Persist Data in Containers

Docker Volumes Explained: Persist Data in Containers

by Knowledge Base Linux
Docker Volumes Explained — Docker Volumes Explained: Persist Data in Containers

This article covers Docker Volumes Explained in detail. Containers are designed to be disposable — you can delete and recreate them at any time. But what happens to the database files, uploaded images or configuration inside when a container disappears? By default, they vanish with it. Docker volumes solve this problem by storing data outside the container’s writable layer, managed directly by Docker. This guide explains how docker volumes work, how they differ from bind mounts and tmpfs, and how to back them up safely. If Docker is not set up yet, begin with our Ubuntu 24.04 Docker installation guide.

How Docker Volumes Work – Docker Volumes Explained

A volume is a directory on the host (under /var/lib/docker/volumes/ on Linux) that Docker mounts into one or more containers. Because the data lives outside the container filesystem, it survives container restarts, upgrades and removals. Volumes are the preferred mechanism for persistent data in Docker because they are portable across hosts, easy to back up, and perform at native filesystem speed.

Backing up docker volumes with a helper container

Creating and Using Docker Volumes – Docker Volumes Explained

# Create a named volume
docker volume create appdata

# List volumes
docker volume ls

# Inspect a volume (see its host path)
docker volume inspect appdata

# Run a container with the volume mounted
docker run -d --name db \
  -v appdata:/var/lib/mysql \
  -e MYSQL_ROOT_PASSWORD=secret \
  mysql:8.0

With the modern flag syntax, the equivalent is --mount source=appdata,target=/var/lib/mysql. The --mount form is more explicit and errors out if you mistype a path, which makes it safer in scripts.

You can also let Docker create the volume automatically the first time a container references it — that is exactly what happens with volumes declared in a Compose file.

Volumes vs Bind Mounts vs tmpfs

  • Named volumes — managed by Docker, best default choice for application data such as databases and uploads.
  • Bind mounts — map an exact host path into the container, e.g. -v /srv/config:/etc/app:ro. Ideal for configuration files and for development, where you want live code changes without rebuilding.
  • tmpfs mounts — stored in RAM only and never written to disk, e.g. --tmpfs /run/secrets. Useful for sensitive temporary data.

A common production pattern combines all three: a named volume for the database, a read-only bind mount for configuration, and tmpfs for runtime scratch space.

Sharing Data Between Containers

Multiple containers can mount the same volume, which is handy for sidecar patterns — for example, a backup container that reads the same data directory as your application:

docker run --rm \
  -v appdata:/data:ro \
  alpine tar czf /backup/appdata-$(date +%F).tar.gz -C /data .

Be careful with concurrent writers: volumes do not provide locking, so applications that write to shared files must coordinate among themselves, just as they would on a normal filesystem.

Backing Up and Restoring a Volume

The reliable way to back up a volume is through a throwaway container that mounts it read-only and archives it to a bind-mounted backup folder:

# Backup
docker run --rm \
  -v appdata:/data:ro \
  -v $(pwd)/backups:/backup \
  alpine tar czf /backup/appdata.tar.gz -C /data .

# Restore into a fresh volume
docker volume create appdata-restored
docker run --rm \
  -v appdata-restored:/data \
  -v $(pwd)/backups:/backup \
  alpine sh -c "cd /data && tar xzf /backup/appdata.tar.gz"

For databases, prefer logical backups (mysqldump, pg_dump) for consistency — a raw file copy taken while the database is writing can produce a corrupt archive.

Named vs Anonymous Volumes

If you mount a path without naming the volume — for example -v /var/lib/mysql with no source — Docker creates an anonymous volume identified only by a random hash. These work fine technically, but they are hard to identify later and are the main source of orphaned data during cleanup. Always name volumes you care about: a readable name like appdata makes backups, inspections and migrations dramatically easier months down the line.

Cleaning Up Volumes

# Remove one volume (container must be stopped/removed first)
docker volume rm appdata

# List dangling volumes
docker volume ls -f dangling=true

# Remove all unused volumes (asks for confirmation)
docker volume prune

Anonymous volumes left behind by deleted containers are the usual source of silent disk growth, so check dangling volumes whenever a server’s disk usage climbs unexpectedly.

Troubleshooting

Permission denied inside the container. The volume’s files are owned by the UID the container process runs as. Match the user in the image, or fix ownership once with a root container: docker run --rm -v appdata:/data alpine chown -R 999:999 /data.

Volume appears empty. When you mount a volume over a directory that already contains files in the image, Docker copies image content into a new empty volume on first use — but a pre-existing volume hides the image content. Inspect what is mounted with docker inspect before assuming data loss.

Cannot remove a volume. It is still referenced by a (possibly stopped) container. Find it with docker ps -a --filter volume=appdata.

Conclusion

Docker volumes keep your data independent from the container lifecycle: create named volumes for databases and uploads, bind-mount configuration read-only, and archive volumes regularly with a helper container. Master these basics and container upgrades stop being scary. The official Docker volumes documentation covers advanced drivers and options when you need them.

You may also like

KbLinux

Typically replies within a day

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