Mounting a Volume Inside Docker Container

31 Oct, 2020
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:
To display all the existing Docker Volumes, you can use the list command as follows.
sudo docker volume ls 

Volume List
To create a new Docker Volume, you can use the Volume Create Command.
sudo docker volume create geeksforgeeks

Volume Create
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
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
 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
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.
Docker Container
linux
Advanced Computer Subject
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Fuzzy Logic | Introduction
Game Playing in Artificial Intelligence
Artificial Intelligence - Temporal Logic
Difference between Backward and Forward Chaining.
Markov Decision Process
Getting Started with System Design
Basics of API Testing Using Postman
Web Mining
Python | Implementation of Polynomial Regression
ML | What is Machine Learning ?