Here’s a complete tutorial on docker volume
, covering what it does, examples, and use cases.
What is docker volume
?
docker volume
is a Docker command used to manage volumes, which are Docker’s preferred method for persisting data generated or used by containers. Volumes provide a way to store data that is independent of the container’s lifecycle, ensuring data is not lost when containers are stopped or removed.
Key Features:
- Persistent storage for Docker containers.
- Works with both local storage and remote storage drivers.
- Supports sharing data between containers.
- Helps manage data outside the container filesystem.
Basic Syntax
docker volume [COMMAND]
Common docker volume
Commands:
create
: Create a new volume.ls
: List all volumes.inspect
: Display detailed information about a volume.rm
: Remove a volume.prune
: Remove all unused volumes.
Examples of docker volume
Commands
1. List All Volumes
docker volume ls
Example Output:
DRIVER VOLUME NAME
local my_volume
local data_backup
2. Create a New Volume
docker volume create my_volume
This creates a new volume called my_volume
.
3. Create a Volume with a Specific Label
docker volume create --label backup=true my_backup_volume
This creates a volume with a label backup=true
.
4. Inspect a Volume
docker volume inspect my_volume
Example Output:
[
{
"CreatedAt": "2025-02-07T10:00:00Z",
"Driver": "local",
"Labels": {},
"Mountpoint": "/var/lib/docker/volumes/my_volume/_data",
"Name": "my_volume",
"Scope": "local"
}
]
5. Remove a Volume
docker volume rm my_volume
This removes the my_volume
volume.
6. Remove All Unused Volumes
docker volume prune
This removes all dangling volumes (volumes not associated with any container).
7. Use a Volume When Running a Container
docker run -d --name my_app --mount source=my_volume,target=/app/data my_image
This runs my_app
with my_volume
mounted at /app/data
.
8. Bind-Mount a Local Directory as a Volume
docker run -d --name my_app -v /path/on/host:/app/data my_image
This mounts the host directory /path/on/host
into the container at /app/data
.
9. Share a Volume Between Multiple Containers
docker run -d --name app1 --mount source=my_shared_volume,target=/shared my_image
docker run -d --name app2 --mount source=my_shared_volume,target=/shared my_image
Both app1
and app2
can read and write to my_shared_volume
.
10. Backup a Volume
docker run --rm -v my_volume:/data -v $(pwd):/backup alpine tar cvf /backup/my_volume_backup.tar /data
This creates a backup of my_volume
as my_volume_backup.tar
.
11. Restore a Volume from Backup
docker run --rm -v my_volume:/data -v $(pwd):/backup alpine tar xvf /backup/my_volume_backup.tar -C /data
This restores my_volume
from the backup file.
12. Use Named Volumes in Docker Compose
docker-compose.yml
example:
version: "3"
services:
web:
image: nginx
volumes:
- my_volume:/usr/share/nginx/html
volumes:
my_volume:
This sets up a persistent volume my_volume
for the web
service.
Use Cases for docker volume
1. Persistent Data Storage
- Store application data (e.g., database files, logs) that needs to persist beyond the container’s lifecycle.
- Example: Use a volume for MySQL to persist database data.
2. Sharing Data Between Containers
- Share data between multiple containers using a common volume.
- Example: Share configuration files between a web server and a reverse proxy.
3. Backup and Restore Data
- Create backups of volumes for disaster recovery or data migration.
- Example: Backup a PostgreSQL data volume before upgrading the database.
4. Simplified Configuration Management
- Use volumes to store configuration files and secrets.
- Example: Store TLS certificates for a web server in a volume.
5. Optimizing CI/CD Pipelines
- Use volumes to cache dependencies between builds to speed up CI/CD pipelines.
- Example: Cache npm or Maven dependencies in a named volume.
6. Logs and Application Data Collection
- Store logs and output files in a volume for easy access and analysis.
- Example: Use a volume to collect logs from a web server.
List of Common docker volume
Commands
Command | Description |
---|---|
docker volume ls | List all volumes |
docker volume create my_volume | Create a new volume |
docker volume inspect my_volume | Inspect a volume for details |
docker volume rm my_volume | Remove a specific volume |
docker volume prune | Remove all unused volumes |
docker run --mount source=my_volume,target=/app/data my_image | Use a volume in a container |
docker run -v /host/path:/container/path my_image | Bind-mount a host directory as a volume |
Best Practices for Using Docker Volumes
- Use named volumes for persistent storage instead of bind mounts for portability and easier management.
- Regularly prune unused volumes to avoid consuming disk space.
- Backup important volumes before upgrading Docker or containers.
- Use labels to organize and identify volumes.
- Inspect volumes to monitor storage usage and location (
Mountpoint
).
Common Errors and Solutions
- “Volume not found”
→ Ensure the volume exists by runningdocker volume ls
. - “Cannot remove volume in use”
→ Stop and remove the container using the volume before deleting it:docker rm -f my_container docker volume rm my_volume
- “Permission denied”
→ Ensure the correct file permissions on the host directory for bind mounts. - “Volume takes too much disk space”
→ Usedocker system df
to analyze disk usage and remove unused volumes withdocker volume prune
.
Combining docker volume
with Other Commands
Monitor and Manage Volume Disk Usage
docker system df -v
Automate Volume Backup and Pruning
#!/bin/bash
docker volume prune -f
docker run --rm -v my_volume:/data -v $(pwd):/backup alpine tar cvf /backup/backup.tar /data
I’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I am working at Cotocus. I blog tech insights at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at I reviewed , and SEO strategies at Wizbrand.
Please find my social handles as below;
Rajesh Kumar Personal Website
Rajesh Kumar at YOUTUBE
Rajesh Kumar at INSTAGRAM
Rajesh Kumar at X
Rajesh Kumar at FACEBOOK
Rajesh Kumar at LINKEDIN
Rajesh Kumar at PINTEREST
Rajesh Kumar at QUORA
Rajesh Kumar at WIZBRAND
fsdfsf
sdsadad