Skip to content
Velloreth
Developer Tools 5 min read

Docker Volumes Explained: Persisting Data in Containers

A container's filesystem is ephemeral. Learn how volumes, bind mounts and tmpfs differ and which one to use for what kind of data.

By

Why a container’s filesystem is not enough

A container has a writable layer on top of its image. A write to a file in that layer is stored in the layer, and the layer is deleted when the container is removed.

docker run --name db postgres
# ... write some data ...
docker rm db
# The data is gone

The writable layer is ephemeral by design. It is what makes a container disposable: you can remove it and start a fresh one from the image, and the state is reset.

Persistent data needs to live outside the layer. That is what a volume, a bind mount, or a tmpfs provides.

The three options

A volume is managed by Docker and stored in Docker’s area, usually /var/lib/docker/volumes on Linux. It is the recommended choice for persistent data.

A bind mount maps a specific host path into the container. It is the right choice for development, where you want to edit a file on the host and see the change immediately.

A tmpfs mount is in memory. It is the right choice for a secret or a cache that must not touch the disk.

Volumes

Create and use a named volume.

docker volume create pgdata
docker run -d --name db -v pgdata:/var/lib/postgresql/data postgres

The syntax is name:path. The name is the volume, and the path is where it is mounted in the container.

A named volume survives a container removal. Removing the container leaves the volume, and a new container can mount the same volume.

docker rm -f db
docker run -d --name db2 -v pgdata:/var/lib/postgresql/data postgres
# The data is still there

An anonymous volume is created when you give only a path. It has a random name, and it is easy to lose track of.

docker run -v /var/lib/postgresql/data postgres
# Creates a volume with a hash name

In a Dockerfile, VOLUME declares an anonymous volume. It is a hint that the path holds data, and it has the side effect of creating an anonymous volume on every run.

Bind mounts

Map a host path into the container.

docker run -v "$(pwd)/src:/app/src" node:20 npm run dev

The syntax is hostpath:containerpath. A path that starts with / or . is a bind mount; a name is a volume.

This is the development pattern. The source is on the host, the editor edits the host file, and the container sees the change without a rebuild.

The downside is the host dependency. The path must exist, the permissions must match, and the behaviour differs between Linux, macOS and Windows. A bind mount is not portable, which is why it is not the choice for production data.

--mount is the explicit form. It is more verbose and less error-prone, because it names each part.

docker run --mount type=bind,source="$(pwd)/src",target=/app/src node:20

tmpfs

Mount a memory-backed filesystem.

docker run --tmpfs /tmp:rw,size=64m node:20

The data is in memory and is not written to disk. It disappears when the container stops.

Use it for a secret that should not be written to a disk, and for a cache that is cheap to rebuild. Do not use it for data that must survive a restart.

Volumes in Compose

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

volumes:
  pgdata:

The top-level volumes block declares the named volume. A service that references it mounts it.

docker compose down leaves the volume. docker compose down -v removes it, which deletes the data. The -v flag is the one to be careful with.

A bind mount in Compose uses a relative path.

    volumes:
      - ./src:/app/src

Sharing data between containers

Two containers can mount the same volume. This is how a sidecar reads a log directory, or how a backup container reaches a database’s files.

docker run -d --name app -v shared:/data app
docker run --rm -v shared:/data backup tar czf /backup/data.tgz /data

A read-only mount is safer. Add :ro to prevent a container from writing.

docker run -v pgdata:/data:ro backup

Backing up and restoring

A volume is not a file you can copy. The way to reach it is a container that mounts it.

# Back up
docker run --rm \
  -v pgdata:/data:ro \
  -v "$(pwd):/backup" \
  alpine tar czf /backup/pgdata.tgz -C /data .

# Restore
docker run --rm \
  -v pgdata:/data \
  -v "$(pwd):/backup" \
  alpine tar xzf /backup/pgdata.tgz -C /data

The pattern is a temporary container that mounts both the volume and a host directory, and copies between them.

Common mistakes

Storing a database in the writable layer. The data is lost when the container is removed. Use a volume.

Using a bind mount in production. It depends on the host’s path and permissions, and it is not portable.

docker compose down -v by accident. It deletes the volumes and the data. Use it deliberately.

An anonymous volume that is never cleaned up. docker volume prune removes the unused ones, and it is worth running periodically.

Editing the files in /var/lib/docker/volumes directly. Docker manages the directory and the metadata. Use a container.

A bind mount with the wrong permissions. The container’s user may not be able to read the host file, which produces a permission error that is not obvious.

Assuming a volume is a backup. A volume on the same host is not a backup. Copy the data out.

A quotable summary

A container’s writable layer is deleted when the container is removed, so persistent data must live in a volume, a bind mount, or a tmpfs. A volume is managed by Docker and is the right choice for production data; a bind mount maps a host path and is the right choice for development; a tmpfs is in memory and is the right choice for a secret or a cache. A named volume survives a container removal, and docker compose down -v deletes it. Back up a volume with a temporary container that mounts it and copies the data out.

For the container basics, see docker-basics-guide and for the multi-container setup, docker-compose-guide. For the broader context, docker-networking-basics and docker-images-vs-containers.

Frequently asked questions

What is a Docker volume?
A Docker volume is a storage location managed by Docker, stored outside the container's writable layer. It persists when the container is removed, and it can be shared between containers. It is the recommended way to store data that must survive a container's lifecycle, such as a database's files.
What is the difference between a volume and a bind mount?
A volume is managed by Docker and lives in Docker's storage area, so it is portable and does not depend on the host's directory layout. A bind mount maps a specific host path into the container, so it depends on the host and is the right choice for development, where you want to edit a file on the host and see the change in the container.
Where are Docker volumes stored on the host?
On Linux, in /var/lib/docker/volumes by default. You should not edit the files there directly, because Docker manages the directory and the metadata. Use docker volume inspect to find the mount point, and use a container to read or write the data.
Do volumes survive docker compose down?
Yes, by default. docker compose down removes the containers and the network but leaves the named volumes. docker compose down -v removes the volumes too, which deletes the data. The -v flag is the one to be careful with.
How do I back up a Docker volume?
Run a temporary container that mounts the volume and a host directory, and copy the data out with tar. The same pattern restores it. The volume is not a file you can copy directly, so the container is the way to reach it.
When should I use tmpfs?
Use tmpfs for data that must not be written to disk, such as a decrypted secret or a session cache, and for data that is expensive to write and cheap to lose. It lives in memory, so it is fast and it disappears when the container stops.