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).
1. Creating Docker Volume
To create a Docker Volume, you can use the Volume Create command as shown below.
sudo docker volume create vol-demo

2. Mounting Volume with a Container
After you have created a Volume, you can mount it with a Docker Container -v flag along with the Docker run command.
sudo docker run -it -v <volume-path-in-local-machine>:<dest-path-in-container> <image-name>

To verify if the volume has been successfully mounted or not, you can move to the destination directory inside the Container.

3. Listing all the Docker Volumes
You can list all your Docker Volumes using the Docker Volume ls command.
sudo docker volume ls

4. Inspecting Docker Volumes
You can get the details of your Docker Volumes using the Volume Inspect Command.
sudo docker volume inspect <volume-name>

5. Removing specific Docker Volume
To remove a particular Docker Volume, you can specify the name in the Docker Volume rm command.
sudo docker volume rm <volume-name>

6. Removing all the Docker Volumes
To remove all the Docker volumes together, you can use the following command. Note that before removing a Docker Volume, you need to make sure that it is not mounted to any Container.
sudo docker volume rm $(sudo docker volume ls -q)

To verify whether all the volumes have been deleted or not, you can use the Volume list command.
sudo docker volume ls
