Docker Volume Commands
| Command | Description | Action |
|---|---|---|
| docker volume create [volumeName] | Creates a new volume for persistent data storage | |
| docker volume ls | Lists all volumes available on your system | |
| docker volume inspect [volumeName] | Displays detailed information about a specific volume | |
| docker volume rm [volumeName] | Deletes a specific volume | |
| docker volume prune | Deletes all unused volumes (not currently mounted by any container) |
Practical Volume Examples
Creating and Using a Volume
# Create a new volume
docker volume create myapp-data
# Run a container with the volume mounted
docker run -v myapp-data:/app/data myapp-image
# Data written to /app/data will persist
docker volume create myapp-data
# Run a container with the volume mounted
docker run -v myapp-data:/app/data myapp-image
# Data written to /app/data will persist
Cleaning Up Volumes
# List all volumes
docker volume ls
# Remove a specific volume
docker volume rm myapp-data
# Remove all unused volumes
docker volume prune
docker volume ls
# Remove a specific volume
docker volume rm myapp-data
# Remove all unused volumes
docker volume prune
Volume Management Tips
Naming Volumes
Use descriptive names for your volumes to easily identify their purpose. For example:
postgres-data,
app-uploads
Volume Inspection
Use docker volume inspect to see where the volume is stored on your host system and other details.
Important Warning
docker volume prune will permanently delete all volumes that are not currently in use by any container. Use with caution!
Quick Reference
When to Use Each Command
- docker volume create - When starting a new application that needs persistent storage
- docker volume ls - To see what volumes exist on your system
- docker volume inspect - To troubleshoot volume issues or find where data is stored
- docker volume rm - To clean up a specific volume you no longer need
- docker volume prune - To clean up all unused volumes and free up disk space