Docker Compose — Volume Lifecycle

2 minute read

Understanding when volumes are created, preserved, and removed is critical for data safety. This page covers the complete volume lifecycle.


Volume Lifecycle Diagram

docker compose up        docker compose down       docker compose down -v
      │                        │                          │
      ▼                        ▼                          ▼
┌─────────────┐          ┌─────────────┐          ┌─────────────┐
│ Volume      │          │ Volume      │          │ Volume      │
│ CREATED     │          │ KEPT ✅     │          │ REMOVED ❌  │
│ (if not     │          │ (data safe) │          │ (data lost) │
│  existing)  │          │             │          │             │
└─────────────┘          └─────────────┘          └─────────────┘

Creating Volumes

Volumes are created automatically when you run docker compose up:

docker compose up -d
[+] Running 3/3
 ✔ Network myapp_default  Created
 ✔ Volume myapp_pgdata    Created      ← Volume created
 ✔ Container myapp-db-1   Started

If the volume already exists, Compose reuses it — no data is lost.


Listing Volumes

# List all Docker volumes
docker volume ls
DRIVER    VOLUME NAME
local     myapp_pgdata
local     myapp_redis-data
# Filter by project
docker volume ls --filter label=com.docker.compose.project=myapp

Inspecting Volumes

docker volume inspect myapp_pgdata
[
    {
        "CreatedAt": "2025-01-01T00:00:00Z",
        "Driver": "local",
        "Mountpoint": "/var/lib/docker/volumes/myapp_pgdata/_data",
        "Name": "myapp_pgdata",
        "Scope": "local"
    }
]

Removing Volumes

With docker compose down -v

# ⚠️ This removes ALL named volumes defined in the compose file
docker compose down -v
[+] Running 3/3
 ✔ Container myapp-db-1   Removed
 ✔ Volume myapp_pgdata    Removed      ← Volume removed!
 ✔ Network myapp_default  Removed

Manual Removal

# Remove a specific volume
docker volume rm myapp_pgdata

# Remove all unused volumes (⚠️ dangerous)
docker volume prune

Volume Removal Rules

Action Named Volumes Anonymous Volumes
docker compose down Kept ✅ Removed
docker compose down -v Removed ❌ Removed
docker compose rm Kept ✅ Removed
docker volume prune Removed if unused ❌ Removed ❌

Backing Up Volume Data

Since volumes are Docker-managed, you need a container to access the data:

# Backup a volume to a tar file
docker run --rm \
  -v myapp_pgdata:/data \
  -v $(pwd):/backup \
  alpine tar czf /backup/pgdata-backup.tar.gz -C /data .

# Restore from backup
docker run --rm \
  -v myapp_pgdata:/data \
  -v $(pwd):/backup \
  alpine sh -c "cd /data && tar xzf /backup/pgdata-backup.tar.gz"

Summary

Operation Volumes
docker compose up Created (if new)
docker compose down Kept (safe)
docker compose down -v Removed (data lost!)
docker volume prune Removes unused volumes
Backup Use a helper container with tar

Quick Reference

Item Detail
Create Automatic on docker compose up
Preserve docker compose down keeps volumes
Remove docker compose down -v removes volumes
List docker volume ls
Inspect docker volume inspect
Backup Use alpine container with tar