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]
Code language: CSS (css)
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
Code language: JavaScript (javascript)
This creates a volume with a label backup=true.
4. Inspect a Volume
docker volume inspect my_volume
Example Output:
[
{
"CreatedAt": "2026-02-07T10:00:00Z",
"Driver": "local",
"Labels": {},
"Mountpoint": "/var/lib/docker/volumes/my_volume/_data",
"Name": "my_volume",
"Scope": "local"
}
]
Code language: JSON / JSON with Comments (json)
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
Code language: JavaScript (javascript)
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
Code language: JavaScript (javascript)
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
Code language: JavaScript (javascript)
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
Code language: JavaScript (javascript)
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:
Code language: JavaScript (javascript)
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 dfto 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
Code language: JavaScript (javascript)
I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms.
I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.
Find Trusted Cardiac Hospitals
Compare heart hospitals by city and services โ all in one place.
Explore Hospitals