Using CLI to Manage Docker Volumes (original) (raw)

Last Updated : 23 Jul, 2025

If you want to share files and directories among multiple Docker Containers, you can easily mount Docker Volumes to different Containers. However, managing such a large number of Docker Volumes becomes very difficult at times. In this article, we are going to discuss how to manage Docker Volumes by executing commands through the Docker Command Line Interface (CLI).

Table of Content

What are Docker Volumes?

Docker volumes are the ones meant to provide a persistent storage mechanism. It is used for managing data and storing them outside the container's system ensuring it data persistence even when the container is removed or recreated. These are essential for maintaining data consistency and sharing the data between multiple containers. Docker volumes add features to docker for managing stateful applications in docker.

Why use Docker Volumes?

The following are the reasons to use docker volumes:

Commands of Docker Volumes

The following are some of the commands of docker volumes:

Purpose Docker Command
To create a docker volume docker volume create
To list all the docker volumes present in the docker docker volume ls
To inspect the docker volume docker volume inspect
To remove the docker volume docker volume rm
To remove all the unused volumes docker volume prune
To attach the volumes to the running in the docker container. docker run -v :
To copy the data between the containers docker cp :

How Does Volume Work in Docker?

Docker volumes are used to store the data which is produced by the application which is containerized in the docker All the data will be stored in the docker of the dedicated directory on the host system.

/var/lib/docker/volumes

Volumes in the docker will be mounted to the particular host path of the application where the data of the application is going to the stored. This implies that since the volume is kept independently on the host and may be directly accessed with manual tools or remounted to another container, the written data will remain accessible even if the container terminates. To know more about docker commands refer to the Docker cheat sheet.

Examples to Implement Docker Volume Commands

The following are the examples to implement docker volume commands:

1. Creating Docker Volume

sudo docker volume create vol-demo

 Creating Docker Volume

2. Mounting Volume with a Container

sudo docker run -it -v :

Mounting Volume with a Container

move to the destination directory

3. Listing all the Docker Volumes

sudo docker volume ls

Listing all the Docker Volumes

4. Inspecting Docker Volumes

sudo docker volume inspect

 Inspecting Docker Volumes

How to Removing specific Docker Volume?

For removing a specific volume in docker, execute the following docker volume rm command with specifying the volume name.

sudo docker volume rm

Removing specific Docker Volume

How to Removing all the Docker Volumes?

The following steps guides you on how to remove all the docker volumes:

**Step 1: Remove all the Docker Volumes

sudo docker volume rm $(sudo docker volume ls -q)

Removing all the Docker Volumes

**Step 2: Verify Deleted Volumes

sudo docker volume ls

verify deletion

Advantages of Docker Volumes

The following are the advantages of Docker volumes:

How do you clean up docker volumes?

The following steps guides you on how to clean up the docker volumes:

**Step 1: Firstly list all the docker volumes with the following command

docker volume ls

Listing docker volumes

**Step 2: To remove all the unused volumes with docker volume prune with the following command

docker volume prune

docker volume prune

**Step 3: For removing specific volumes in the docker specify the volume name with docker volume rm command as follows

docker volume rm

**Step 4: To associated volumes with the stopped containers using following command

docker rm -v

Why to clean Docker Volumes?

The following are the reasons and advantages to clean up docker volumes: