Find the Best Cosmetic Hospitals

Explore trusted cosmetic hospitals and make a confident choice for your transformation.

โ€œInvest in yourself โ€” your confidence is always worth it.โ€

Explore Cosmetic Hospitals

Start your journey today โ€” compare options in one place.

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 a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I have worked at <a href="https://www.cotocus.com/">Cotocus</a>. I share tech blog at <a href="https://www.devopsschool.com/">DevOps School</a>, travel stories at <a href="https://www.holidaylandmark.com/">Holiday Landmark</a>, stock market tips at <a href="https://www.stocksmantra.in/">Stocks Mantra</a>, health and fitness guidance at <a href="https://www.mymedicplus.com/">My Medic Plus</a>, product reviews at <a href="https://www.truereviewnow.com/">TrueReviewNow</a> , and SEO strategies at <a href="https://www.wizbrand.com/">Wizbrand.</a> Do you want to learn <a href="https://www.quantumuting.com/">Quantum Computing</a>? <strong>Please find my social handles as below;</strong> <a href="https://www.rajeshkumar.xyz/">Rajesh Kumar Personal Website</a> <a href="https://www.youtube.com/TheDevOpsSchool">Rajesh Kumar at YOUTUBE</a> <a href="https://www.instagram.com/rajeshkumarin">Rajesh Kumar at INSTAGRAM</a> <a href="https://x.com/RajeshKumarIn">Rajesh Kumar at X</a> <a href="https://www.facebook.com/RajeshKumarLog">Rajesh Kumar at FACEBOOK</a> <a href="https://www.linkedin.com/in/rajeshkumarin/">Rajesh Kumar at LINKEDIN</a> <a href="https://www.wizbrand.com/rajeshkumar">Rajesh Kumar at WIZBRAND</a> <a href="https://www.rajeshkumar.xyz/dailylogs">Rajesh Kumar DailyLogs</a>

Related Posts

Docker Tutorials: Docker Image – Understanding Dockerfiles instructions & options

Hereโ€™s a step-by-step tutorial for Dockerfile, including explanations and examples for each major command. Dockerfile Tutorial A Dockerfile is a text file containing instructions to build a…

Read More

Docker Tutorials: Docker Image – Example and Sample Programs of Dockerfile

Reference Rajesh Kumar Iโ€™m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I have worked at Cotocus. I share tech blog at DevOps School, travel stories…

Read More

Docker Tutorials: Installation and Configurations

Docker Installation in Centos/RHEL Method -1: How to install Docker Community Edition via YUM? Step 1 – Install required packages. yum-utils provides the yum-config-manager utility, and device-mapper-persistent-data…

Read More

Docker Tutorials: How to Install Docker in Ubuntu?

Install Docker Engine in Ubuntu NOTE – All commands you must run as root user or add a current user into a linux group name called “docker”…

Read More

Docker Lab, Excercise & Assignment – 7 – Docker Volume

Below is a very detailed tutorial and lab manual for learning Docker Volumes, using the Ubuntu image for practical, hands-on labs. This covers all major types of…

Read More

Docker Lab, Excercise & Assignment – 4 – Docker Networking

Hereโ€™s an in-depth, step-by-step tutorial and lab manual for Docker Networkingโ€”starting from basics, covering all core concepts, and providing a hands-on guide to every feature and command….

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