Mastering Data Mobility: Unveiling the Power of Docker Volumes
MetricsViews Private Limited
Building A Solid DevOps Strategy With Cloud Native.
Docker volume is a storage mechanism that is used for persistent data storage generated by Docker containers.
?
All volumes are managed by Docker and stored in a dedicated directory on your host, usually /var/lib/docker/volumes for Linux systems.
?
These are the uses of Volume:
§? Decoupling containers from storage
§? Attach Volume to a container
§? Share volume (storage/data) among different containers
§? On Deleting container volume do not delete
?
Commands of Docker Volume
?
§? create: it is used to create new volumes.
§? ls: It is used to list all the volumes in a namespace.
§? inspect: It is used to know more about any of the volumes.
§? rm: It is used to remove any volume if it is no longer required.
§? prune: It is used to remove all unused volumes.
?
Examples to Implement Docker Volume
?
You can start a container with a volume by setting the -v flag when you call docker run.
?
1. Create a new volume named ‘praman-vol’ and list it as below.
docker volume create praman-vol
docker volume ls
Output:
2. Check all details of this volume using the ‘inspect’ command as below:
Command:?
docker volume inspect my-vol
Output:
3. Now, mount this volume to a container using the ‘-v’ flag and connect to the container using the ‘docker exec’ command, create a new test file ‘test.txt’. Stop and delete this container and create a new container and connect the same volume to this container. Again, execute this new container and check if the file is available or not in this newly created container as below
?
Commands:
docker run -itd -v praman-vol:/root --name wilsh-harvi alpine sleep 2000
docker exec -it wilsh-harvi sh
/ # cd /root
~ # echo “Welcome to praman” > test.txt
~ # cat test.txt
~ # exit
docker container stop wilsh-harvi
docker container rm wilsh-harvi
docker run -itd --mount source=praman-vol,target=/etc/ --name wilsh-harvi2 alpine sleep 2000
docker exec -it wilsh-harvi2 sh
/ # cat /etc/test.txt
/ # exit
Output:
领英推荐
Explanation: In the above snapshot, we can see that the file is available even after the first container got destroyed.
?
4. If we want to see the above file without mounting it to any container then we have to browse the ‘Mountpoint’ of the volume as below:
Command:
sudo cat /var/lib/docker/volumes/praman-vol/_data/test.txt
?
Output:
5. If this volume is no longer required and can be deleted; we can do so using the ‘rm’ command as below:
Command:
docker volume rm praman-vol
Output:
6. If there are multiple volumes that need to be deleted, we use the ‘prune’ command to delete all unused volumes at once as below:
Command:
docker volume prune
Output:
Explanation: In the above example, we have created 4 new volumes and did not attach any container to them so we can use the ‘prune’ command to delete all three volumes at once.
?
Advantages of Docker Volume
?
§? We can share volumes among multiple containers safely; backup and migration of volumes are also easy.
§? Allows sharing data among multiple containers.
§? We can attach volumes on both types of containers, whether it is a Linux and Windows container.
?
Conclusion
?
To conclude, in this article, we discussed how to create and inspect a Volume and mount it to multiple Docker Containers. This proves to be very helpful when you want shared access to files and directories among multiple Docker Containers.
?
Credit – Sachin Auti (DevOps)
?
MetricsViews Pvt. Ltd.
MetricsViews specializes in building a solid DevOps strategy with cloud-native including AWS, GCP, Azure, Salesforce, and many more.? We excel in microservice adoption, CI/CD, Orchestration, and Provisioning of Infrastructure - with Smart DevOps tools like Terraform, and CloudFormation on the cloud.
?