Using CLI to Manage Docker Volumes
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).
Commands of Docker Volume
Purpose |
Docker Command |
---|---|
To create a docker volume |
docker volume create <Name of the volume> |
To list all the docker volumes present in the docker |
docker volume ls |
To inspect the docker volume |
docker volume inspect <Name of the volume> |
To remove the docker volume |
docker volume rm <Name of the docker volume> |
To remove all the unused volumes |
docker volume prune |
To attach the volumes to the running in the docker container. |
docker run -v <Name of the Volume>:<Path of the volume> |
To copy the data between the containers |
docker cp <Name Of Volume> :<Path><host path> |
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.
Advantages of Docker Volume
- Persistence: The volumes which are attached to the container will be there even if the containers in the docker recreated so the data of the application will not be lost and also with the help of docker volumes you can deploy and maintain the state-full applications such as databases, configuration files, and logs.
- Isolation: If the two state-full application are deployed in the docker then to that two application containers we need to attach the docker volumes in that cases docker can ensure you that there will be no conflict between two volumes it will isolate from each other.
- Portability: The volume which are attached to the docker volumes are portable from one host to the another host or from one environment to another environments. Which make it easy for the data migration between development, staging, and production environments without modifying the container images.
- Dynamic Data Management:Throughout a container’s lifecycle, dynamic data management is made possible by Docker volumes. Data handling is made more flexible and dynamic by the ability to add, edit, and remove data without changing the container itself.
Examples to Implement Docker Volume Commands
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
FAQs On Docker Commands
1. Docker Volume Command in Dockerfile
The VOLUME instruction in a dockerfile is used to a mount point for a volume within the container. And the command is as follows.
VOLUME <path>
2.Docker Volume Command Not Found
The main reason behind this error was Docker CLI not installed.
3. Docker Volume Command Windows
Following is one of the basic docker volume command to create docker volume.
docker volume create <volume_name>
4. Docker Volume Command Syntax
Following is the syntax of docker volume command.
docker volume create <volume_name>
Contact Us