Why Volumes?
Containers are ephemeral — when they stop, data is gone. Volumes persist data beyond the container lifecycle.
Named Volumes
Docker manages the storage location:
# Create
docker volume create mydata
# Use in a container
docker run -d --name db \
-v mydata:/var/lib/postgresql/data \
postgres:16
# List volumes
docker volume ls
# Inspect
docker volume inspect mydataBind Mounts
Map a host directory into the container:
docker run -d --name web \
-v /home/user/website:/usr/share/nginx/html:ro \
nginxThe :ro flag makes it read-only inside the container.
Master this topic with hands-on labs
Go beyond reading — build real projects in sandboxed environments with expert video guidance.
Browse Courses →Docker Compose Volumes
services:
db:
image: postgres:16
volumes:
- pgdata:/var/lib/postgresql/data # Named volume
- ./init.sql:/docker-entrypoint-initdb.d/init.sql # Bind mount
volumes:
pgdata:When to Use Which
| Feature | Named Volume | Bind Mount |
|---|---|---|
| Managed by Docker | Yes | No |
| Performance | Best (especially on Mac) | Native on Linux |
| Portable | Yes | Depends on host paths |
| Pre-populated | Yes (from image) | No |
| Direct host access | Via docker volume inspect | Yes |
Rule of thumb: named volumes for databases and app state, bind mounts for config files and source code (dev).
Backup a Volume
# Backup to tar
docker run --rm \
-v mydata:/source:ro \
-v $(pwd):/backup \
alpine tar czf /backup/mydata-backup.tar.gz -C /source .
# Restore
docker run --rm \
-v mydata:/target \
-v $(pwd):/backup \
alpine tar xzf /backup/mydata-backup.tar.gz -C /targetCopy Data Between Volumes
docker run --rm \
-v source_vol:/from:ro \
-v dest_vol:/to \
alpine sh -c "cp -a /from/. /to/"Get weekly IT automation tips
Docker, Ansible, Terraform, MLOps — curated insights delivered to your inbox. No spam.
Subscribe Free →Share Volumes Between Containers
Multiple containers can mount the same volume:
services:
writer:
image: alpine
command: sh -c "while true; do date >> /data/log.txt; sleep 5; done"
volumes:
- shared:/data
reader:
image: alpine
command: tail -f /data/log.txt
volumes:
- shared:/data:ro
volumes:
shared:tmpfs Mounts
In-memory storage — fast but data is lost on restart:
docker run -d --name cache \
--tmpfs /tmp:size=100m \
redis:7-alpineUse for: temporary files, caches, sensitive data that should not persist.
Cleanup
# Remove unused volumes
docker volume prune
# Remove a specific volume
docker volume rm mydata
# Nuclear — remove all stopped containers and their volumes
docker system prune --volumesWarning: docker volume prune deletes ALL volumes not attached to a running container. Double-check before running in production.
Related Posts
- Getting Started with Docker for Docker basics
- Docker Compose for Dev Environments for multi-service stacks
- OpenClaw Volume Permissions Fix for permission issues
---
Ready to go deeper? Check out our hands-on course: Docker Fundamentals — practical exercises you can follow along on your own machine.
Ready to learn by doing?
Stop reading tutorials — start building. Expert video courses with hands-on labs in real sandboxed environments.
Related Articles
Docker Tutorial for Beginners 2026
Complete Docker tutorial for beginners. Learn containers, images, Dockerfiles, volumes, networking, and Docker Compose step by step.
Podman vs Docker Comparison 2026
Podman vs Docker in 2026: rootless containers, daemonless architecture, native pod support, and when to choose each runtime.
Docker Image Optimization Guide
Reduce Docker image size by up to 90%. Multi-stage builds, layer caching, distroless base images, and build-time best practices.
Domain-Specific AI Models Guide
Build and deploy domain-specific AI models with fine-tuning, RAG, and specialized training data for healthcare, finance, and DevOps applications.
Earthly Reproducible Build Tool
Earthly combines Dockerfiles and Makefiles into reproducible, containerized builds. Learn how Earthly works, how to write Earthfiles, and when it replaces.
Energy-Efficient AI Infrastructure
Build sustainable AI infrastructure with energy-efficient GPU scheduling, green computing practices, carbon-aware workloads, and cooling optimization.
Explore topics
Browse more articles on the topics covered here.