How To Create Volume In Docker
When you are working on a micro-service architecture using Docker Containers, you create multiple Docker Containers to create and test different components of your application. Now, some of those components might require sharing files and directories. If you copy the same files in all the Containers separately, it might lead to an unnecessary increase in the Image size, and also, changing a file in one Container will not create the corresponding change in the same file in other Containers.
Hence, you required a shared directory or volume that you can mount on multiple Docker Containers and all of them have shared access to a particular file or directory. Docker allows you to mount shared volumes in multiple Containers. In this article, we will mount a volume to different Containers and check whether the changes in the file is shared among all the Containers or not. Follow the below steps to mount a volume inside Docker Container:
Step 1: Display all the existing Docker Volumes
To display all the existing Docker Volumes, you can use the list command as follows.
sudo docker volume ls
Volume List
Step 2: Creating a Volume
To create a new Docker Volume, you can use the Volume Create Command.
sudo docker volume create geeksforgeeks
Volume Create
Step 3: Inspecting Docker Volumes
To get the details of the Volumes you have created you can, you can use the Volume Inspect Command.
sudo docker volume inspect geeksforgeeks
Volume Inspect
Step 4: Mounting Docker Volumes
After creating the Volume, the next step is to mount the Volume to Docker Containers. We will create a Docker Container with the Ubuntu base Image and mount the geeksforgeeks Volume to that Container using the -v flag.
sudo docker run -it -v geeksforgeeks:/shared-volume --name my-container-01 ubuntu
The above command mounts the geeksforgeeks volume to a directory called shared-volume inside the Docker Container named my-container-01.
Mounting Docker Volumes
Step 5: Create a file inside the Docker Volume
Inside the bash of the Container, create a new file and add some content.
ls cd /shared-volume echo "GeeksforGeeks" > geeksforgeeks.txt ls exit
Creating a file
Step 6: Create another Container and Mount the Volume
Create another Docker Container called my-container-02 and mount the same Docker Volume called geeksforgeeks inside the Container.
sudo docker run -it -v geeksforgeeks:/shared-volume --name my-container-02 ubuntu
Verifying Shared Contents
If you go to the shared-volume directory and list the files, you will find the geeksforgeeks.txt file that you had created in the same volume but mounted in my-container-01 earlier and it also has the same content inside it. This is so because, the volume is shared among the two Containers.
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.
How To Create Volume In Docker
Source: https://www.geeksforgeeks.org/mounting-a-volume-inside-docker-container/
Posted by: kirbythimakeent.blogspot.com
0 Response to "How To Create Volume In Docker"
Post a Comment