MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

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]
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

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
Code language: JavaScript (javascript)

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services โ€” all in one place.

Explore Hospitals
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.

Related Posts

Install Jenkins with Docker: Persistent Setup, Port Mapping, and Logs

Learn how to run Jenkins in Docker with correct port mapping, persistent storage, and operational commands. This updated guide keeps the original workflow but adds modern, production-minded practices.

Read More

Install Atlassian Jira with Docker: Quick Start, Persistent Volumes, and Deployment Checks

This guide shows how to run Jira in Docker using the original one-line command, then improve it with persistent storage and repeatable deployment steps. It also includes a workflow diagram, command examples, and practical validation checks.

Read More

Docker Tutorials: Remove all Docker Containers, all Images, all Volumes, and all Networks

How to stop all Docker Containers To delete all containers including its volumes use How to remove all stopped Docker Containers How to delete all Docker Images…

Read More

Docker Interview Questions and Answer Part โ€“ 26

What Docker Client command can be used to convert a Dockerfile into an Image? docker createimage docker buildimage docker build (Ans) docker create Which of the following…

Read More

Docker Interview Questions and Answer Part โ€“ 10

What is a dangling volume? The default volume for the container file system that’s unpacked from the image A volume that’s removed from a running container A…

Read More

Debug for Docker Container

To debug a Docker container, check its state and logs first, then its network configuration, then test connectivity from another container on the same network. This page…

Read More
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
0
Would love your thoughts, please comment.x
()
x