Mounting a volume inside docker container

DockerOperating SystemOpen Source

In some projects, there might be scenarios where you have created multiple containers for different parts of the project and some of those containers share common files as well. Now, you want to create a shared directory between all the containers such that from all the containers you can access that directory or volume and perform changes in the files in that directory from any container.

You can do so, be creating a volume and mounting it to all the containers. By doing so, all the containers will have shared access to the particular volume and you will be able to access and modify files in that volume.

In this article, we will create a volume and mount it to two different containers. We will then create a file inside the volume and will access the same file through both the containers. We will also try deleting the containers and then access the same file through another container.

Steps

To display all the existing volumes, use the following command.

sudo docker volume ls

Now, create a new volume with any name (Eg. tutorialspoint) using the following command.

sudo docker volume create tutorialspoint

You can also inspect the volume that you just created using the following command.

sudo docker volume inspect tutorialspoint

Now, we will create a container (Eg. container01) using base image ubuntu and mount the tutorialspoint volume to that container and access that container using the bash.

sudo docker run -it -v tutorialspoint:/sharedVol --name container01 ubuntu

In the above command, -i flag is used to display the bash in an interactive shell, -v flag is used to mount the tutorialspoint volume that you just created to a new directory called sharedVol which is created inside the container01.

Now, we will try to create a new file inside the /sharedVol directory through the container’s bash. Inside the bash, type the following commands one by one.

ls
cd /sharedVol
echo “Welcome to docker tutorialspoint” > dockertutorials.txt
ls
exit

The ls command will list all the directories inside the container. You will also find the sharedVol directory there. Move to that directory and create a file (dockertutorials.txt) with a message “Welcome to docker tutorialspoint”. Check that the file has been created using the ls command.

Now, exit the bash. Create another container (Eg. container02) and mount the same volume there also.

sudo docker run -it -v tutorialspoint:/sharedVol --name container02 ubuntu

Inside the bash of this container, go to the sharedVol directory and you will find the file which you created in container01. You can use the following commands.

cd sharedVol
ls
cat dockertutorials.txt
exit

Now, if you delete both the containers and create a new container and mount the tutorialspoint volume, you will find the file created there. Let’s try it using the following commands.

sudo docker rm -f container01 container02
sudo docker ps -a
sudo docker run -it -v tutorialspoint:/sharedVol --name container03 ubuntu

The above commands deletes the two containers, lists the existing containers to verify that the containers have been deleted and creates a new ubuntu image container container03 and mounts the tutorialspoint volume inside it.

ls
cd sharedVol
ls
cat dockertutorials.txt

You will find that the same file exists there also.

To conclude, if you maintain multiple containers for your project and you want a shared directory or volume for all the containers, you can mount a new volume using the steps mentioned in this article.

raja
Published on 01-Oct-2020 19:40:01
Advertisements