Use volumes to manage persistent data with Docker Compose
A container's own filesystem disappears when the container is removed, so anything your app writes there is lost.
Docker Compose lets you define and run multi-container applications from a single file. One compose.yaml file describes your services, the networks that connect them, and the volumes that store their data, and one command starts everything.
Volumes are the part that's easy to get wrong. A container's own filesystem disappears when the container is removed, so anything your app writes there is lost. Databases, uploads, and caches need a volume to survive restarts, updates, and docker compose down.
This article explains what Docker volumes are, how named and anonymous volumes differ from bind mounts, and how to define, share, inspect, back up, and clean up volumes with Docker Compose. Every example was tested with Docker Engine and Docker Compose v2.
What are Docker volumes?#
A Docker volume is storage that Docker creates and manages outside a container's filesystem. You mount it at a path inside the container, and whatever the container writes to that path is stored in the volume. When you remove or replace the container, the volume and its data stay, and you can mount it into the new container.
Docker stores volumes in its own data directory, which is /var/lib/docker/volumes/ on Linux. On Docker Desktop, that directory is inside the virtual machine that runs Docker, so you don't work with the files directly. Instead, you manage volumes with Docker commands and back them up through a container, as you'll see below.
Docker has three ways to store data outside a container's writable layer:
- Volumes are managed by Docker. They work the same on every host, can be shared between containers, and are the recommended way to persist data such as databases.
- Bind mounts map a file or directory from the host into the container, such as your project's source code. They depend on the host's directory structure, so they're best for development, when you want changes on the host to appear in the container right away.
- tmpfs mounts store data in memory only, and the data is gone when the container stops. They're useful for temporary files that shouldn't be written to disk.
Named and anonymous volumes#
Volumes come in two types: named and anonymous.
Named volumes#
A named volume has a name you choose, which makes it easy to reference, share between containers, and manage. You can create one with the Docker CLI:
docker volume create my_named_volumeThen mount it into a container by its name:
docker run -d -v my_named_volume:/data nginxIf the volume doesn't exist yet, docker run creates it. Named volumes are what you'll use for almost all persistent data, and they're what Docker Compose creates from the top-level volumes key.
Anonymous volumes#
An anonymous volume has no name. Docker gives it a random ID when it creates it. You get one when you mount a volume with only a container path:
docker run -d -v /data nginxThis starts a container from the nginx image and mounts a new anonymous volume at /data. Docker also creates anonymous volumes for paths that an image declares with the VOLUME instruction in its Dockerfile, such as /var/lib/mysql in the official MySQL image, unless you mount a named volume there.
Because anonymous volumes are hard to tell apart, they're best for temporary data. If you start a container with --rm, Docker removes its anonymous volumes along with the container. Otherwise, they stay behind when the container is removed, and you'll need to clean them up.
How to use volumes with Docker Compose#
In Docker Compose, you define volumes in your compose.yaml file, and Compose creates and mounts them when you start your application. Compose also reads the older docker-compose.yml name, but compose.yaml is the preferred name.
Define and mount volumes#
To use a named volume, declare it under the top-level volumes key, then mount it in a service with the volume name, a colon, and the path in the container:
services:
web:
image: nginx
volumes:
- web_data:/usr/share/nginx/html
web-test:
image: nginx
volumes:
- web_data:/usr/share/nginx/html:ro # Shares web_data with web, read-only
db:
image: mysql:9.7
environment:
MYSQL_ROOT_PASSWORD: example
volumes:
- db_data:/var/lib/mysql
volumes:
web_data:
db_data:This file defines two named volumes, web_data and db_data:
- The
webandweb-testservices both mountweb_data, so they see the same files. A file thatwebwrites to /usr/share/nginx/html appears inweb-testtoo. - The
:rosuffix mountsweb_dataread-only inweb-test, so that service can read the files but not change them. Writing to it fails with a "Read-only file system" error. - The
dbservice stores MySQL's data indb_data, so the database survives when the container is removed and recreated.
You don't need a version key at the top of the file. It's obsolete, and Docker Compose ignores it and prints a warning: the attribute version is obsolete, it will be ignored.
Volume names and the project name#
Compose prefixes each volume name with the project name, which defaults to the name of the directory that holds compose.yaml. If the file above is in a directory called myapp, Compose creates volumes called myapp_web_data and myapp_db_data:
docker volume lsDRIVER VOLUME NAME
local myapp_db_data
local myapp_web_dataKeep this in mind when you use Docker commands on a volume Compose created. docker volume inspect db_data fails, because the volume is called myapp_db_data.
To give a volume an exact name without the prefix, set name:
volumes:
db_data:
name: my_app_db_dataUse a volume created outside Compose#
By default, Compose creates the volumes in your file and manages them as part of the project. To use a volume that already exists, such as one shared by several projects, mark it as external:
volumes:
shared:
external: true
name: my_shared_volumeCompose never creates or removes an external volume. If it doesn't exist, docker compose up fails with external volume "my_shared_volume" not found, so create it first with docker volume create my_shared_volume.
Bind mounts in Compose#
To mount a directory from your project into a container, use a path that starts with ./ or / instead of a volume name:
services:
web:
image: nginx
volumes:
- ./html:/usr/share/nginx/htmlThis mounts the html directory next to compose.yaml into the container, so changes you make on the host show up in the container immediately. Bind mounts don't need an entry under the top-level volumes key.
You may also see a named volume that points to a host directory through the local driver:
volumes:
db_data:
driver: local
driver_opts:
type: none
device: /data/db_data
o: bindThis works like a bind mount with a volume name, but the host directory must already exist. If /data/db_data is missing, the container fails to start with a no such file or directory error. Unless you need a volume name for a host directory, a regular named volume or bind mount is simpler.
How Compose commands affect volumes#
The Compose commands you use every day treat volumes differently:
docker compose upcreates any named volumes that don't exist yet, then starts your services with the volumes mounted. If a volume already exists, Compose reuses it and its data.docker compose downstops and removes your containers and networks, but keeps your named volumes. When you rundocker compose upagain, your data is still there.docker compose down --volumes, or-v, also removes the named volumes declared in your file and the anonymous volumes attached to your containers. Use it when you want to start from scratch, because the data is deleted. It doesn't remove external volumes.docker compose configvalidates your file and prints the final configuration, including volume definitions, which helps you catch mistakes before you start anything.
To see this in practice, we created a table in the MySQL service above, ran docker compose down and docker compose up -d, and the table and its rows were still there. After docker compose down -v, both volumes were removed.
How to manage volumes with Docker commands#
Compose creates regular Docker volumes, so you can manage them with the docker volume commands. For more commands, see our guide to Docker commands.
List volumes#
To list all volumes on your machine, run:
docker volume lsTo list only the volumes of one Compose project, filter by the label Compose adds to them:
docker volume ls --filter label=com.docker.compose.project=myappInspect a volume#
To see the details of a volume, such as its driver, labels, and where Docker stores it, use inspect with the full volume name:
docker volume inspect myapp_db_dataThe output is JSON:
[
{
"CreatedAt": "2026-11-30T17:24:00Z",
"Driver": "local",
"Labels": {
"com.docker.compose.project": "myapp",
"com.docker.compose.version": "2.31.0",
"com.docker.compose.volume": "db_data"
},
"Mountpoint": "/var/lib/docker/volumes/myapp_db_data/_data",
"Name": "myapp_db_data",
"Options": null,
"Scope": "local"
}
]Back up and restore a volume#
Since you can't always reach a volume's files directly, especially on Docker Desktop, the usual way to back one up is to mount it in a temporary container, along with a host directory, and create an archive:
docker run --rm -v myapp_db_data:/data -v "$(pwd)":/backup alpine tar czf /backup/db_data.tar.gz -C /data .This saves the volume's contents to db_data.tar.gz in your current directory. To restore it into a volume, extract the archive the same way:
docker run --rm -v myapp_db_data:/data -v "$(pwd)":/backup alpine tar xzf /backup/db_data.tar.gz -C /dataStop the services that use a volume before you back it up, so that files, especially database files, aren't changing during the copy. For a database, a dump made with its own tools, such as mysqldump, is often a safer backup than copying its files.
Remove a volume#
To remove a volume by name, use docker volume rm:
docker volume rm myapp_web_dataDocker won't remove a volume that a container is using, even a stopped one. Remove the container first, for example with docker compose down.
Clean up unused volumes#
To remove volumes that no container uses, run:
docker volume pruneBy default, prune removes only unused anonymous volumes and keeps named volumes. To remove unused named volumes too, add --all, or -a:
docker volume prune --allBe careful with --all: a named volume is "unused" whenever no container is attached to it, which includes a project you've stopped with docker compose down. Its data is deleted for good.
Docker Compose V1 and V2#
You'll still see docker-compose, with a dash, in older tutorials. That's Docker Compose V1, a separate Python program that stopped receiving updates in July 2023. Docker Compose V2 is a plugin for the Docker CLI, and you run it as docker compose, with a space.
To update old commands, replace the dash with a space, so docker-compose up becomes docker compose up. You can also delete the version key from old files, since V2 ignores it.
Summary#
Volumes keep your containers' data safe when containers are removed, recreated, or updated. In Docker Compose, you declare named volumes under the top-level volumes key, mount them in your services, and Compose creates them with your project name as a prefix. docker compose down keeps your volumes, and docker compose down -v deletes them.
For the rest, the docker volume commands let you list, inspect, back up, and clean up volumes, as long as you remember the full volume name and that prune --all also removes named volumes.
When you move from Docker Compose on your machine to production, Sevalla gives each part of your setup a managed home. Run your app with Application Hosting and give it a persistent disk for files that must survive deployments, the same job a named volume does locally. Move your database service to Database Hosting, which runs managed MySQL, PostgreSQL, and more, with backups included.