Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

Docker commands Guide – docker volume with examples

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

CommandDescription
docker volume lsList all volumes
docker volume create my_volumeCreate a new volume
docker volume inspect my_volumeInspect a volume for details
docker volume rm my_volumeRemove a specific volume
docker volume pruneRemove all unused volumes
docker run --mount source=my_volume,target=/app/data my_imageUse a volume in a container
docker run -v /host/path:/container/path my_imageBind-mount a host directory as a volume

Best Practices for Using Docker Volumes

  1. Use named volumes for persistent storage instead of bind mounts for portability and easier management.
  2. Regularly prune unused volumes to avoid consuming disk space.
  3. Backup important volumes before upgrading Docker or containers.
  4. Use labels to organize and identify volumes.
  5. Inspect volumes to monitor storage usage and location (Mountpoint).

Common Errors and Solutions

  1. “Volume not found”
    → Ensure the volume exists by running docker volume ls.
  2. “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
  3. “Permission denied”
    → Ensure the correct file permissions on the host directory for bind mounts.
  4. “Volume takes too much disk space”
    → Use docker system df to analyze disk usage and remove unused volumes with docker 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

Subscribe
Notify of
guest
2 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.

2
0
Would love your thoughts, please comment.x
()
x