bashbeginner

Docker Volume Management Commands

Essential Docker volume commands for data persistence, backup, migration, and cleanup.

bash
# Create a named volume
docker volume create app-data

# List all volumes
docker volume ls

# Inspect volume details
docker volume inspect app-data

# Run container with named volume
docker run -d --name db \
  -v app-data:/var/lib/postgresql/data \
  postgres:16

# Bind mount (host directory → container)
docker run -d --name app \
  -v $(pwd)/config:/app/config:ro \
  -v $(pwd)/logs:/app/logs \
  myapp:latest

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

# Restore volume from backup
docker run --rm \
  -v app-data:/target \
  -v $(pwd):/backup:ro \
  alpine tar xzf /backup/app-data-backup.tar.gz -C /target

# Copy data between volumes
docker run --rm \
  -v source-vol:/from:ro \
  -v target-vol:/to \
  alpine sh -c 'cp -a /from/. /to/'

# Remove unused volumes
docker volume prune

# Remove specific volume
docker volume rm app-data

Use Cases

  • Persisting database data across container restarts
  • Backing up and restoring container data
  • Migrating data between Docker environments

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.