Docker Volume and how to share it.
Published: 2022-07-12 10:12:58

**Docker** is a common containerization solution that offers a user-friendly interface. It allows you to deploy your application as a lightweight process set rather than a complete virtual machine.


**Docker Volume:-**

A Docker volume is an independent file system entirely managed by Docker and exists as a normal file or directory on the host, where data is persisted.

- Volume is simply a directory made inside our container.

- Even if we stop the container still we can access volume.

- We can declare a directory as a volume only while creating container.

- Cannot create volume for existing container.

- Can share the volume across any number of containers.

Benefits of Volume:-

- Decoupling container from storage.

- Share it among different containers.

- On deleting the container the volume does not get deleted.


We can map volume in two ways:-

1. Container <-> Container

2. Host <-> Container

Created an EC2 Instance-
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w3ik4pcn0wfluyn30yaz.png)
 
 



Then ssh into the instance-

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/blc37qly509svc7g4evq.png)


 
_CREATING VOLUME FROM DOCKER FILE AND MAPPING IT TO ANOTHER CONTAINER:-_

sudo su

yum update -y

Install the Docker:-

yum install docker -y

 ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6o0jm8o028nvwqs0n6ct.png)


 
Create a Docker file:-

vi Dockerfile

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0trf4hbyfqpto9y28kkb.png)

Write the following inside the Dockerfile

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kqna4nbja15ejo6781xl.png)


Then create the image from this Dockefile:-


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eu27bo7diwh45iufhfl7.png)

Now create a container with the name cont1 from the image and run:-


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zgzy8zc00olm4og0z0pj.png)

Create some files in it:-

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ipphm2hq01ozdrwnu1y0.png)


 Now to share the volume with another container, create a container with a name cont2:-

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xafl211zh3oupf3muavx.png)
 

After creating the cont2, the volume from cont1 would be visible and any changes made in that volume will be visible to both the containers.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/repaemi7vghv0ulf6k1o.png)
 
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/25ejm4s3p8er41a2pnm0.png)

_CREATING THE VOLUME USING COMMAND:-
_
Create a new container with name cont3

 ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1f6xsdby9e3pnlwhnvru.png)


Create a new container with the name cont4 and share the volume with it.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h0t7y7cnaxrjii82khyw.png)

 Create some files inside it and see its present in both the containers.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/txhbvc67c8r3hq9419wu.png)


SHARING VOLUME HOST <-> CONTAINER:-

Verify the files in the /home/ec2-user and run the following command -


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wz7nee24yomi2up8eipw.png)
 

Go to that directory and do ls 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hirkavl09s4zo8gq04t1.png)
 
Create some files in the container in that particular directory 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6mqyhxrpvikyj6usojvi.png)

Now check in the EC2 host machine and we can see the files we created now.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d96xtw5vjamrisrforq8.png)