# Day19- 90DaysOfDevOps

Hey Learners! Welcome back. Till now we have learned how to create a docker-compose.yml file and pushed it to the remote repository. Let's move forward and dig more into other important concepts of docker-volume, docker-networking, etc. Let's get started.

### Docker-Volume:-

Volumes are like separate storage areas that can be accessed by containers. Docker allows us to create volumes. With the help of Volumes, we store data, like a database outside the container so that we can persist our container data so that in case of any failure occurs or the container gets restarted container data should not be lost.

There are two ways of storing files in the docker

1. **Volumes** that are part of the host filesystem and managed by Docker. It can be attached to multiple containers.
    
2. **Bind Mounts** which can be stored anywhere in the host system. The files and directories that we are mounting get created if not present on the host machine at the run time.
    

**Commands:-**

* **Creating docker volume:-** To create a docker volume use `docker volume create <vol-name>` command.
    
* **Use volumes in containers:- To attach volumes use one of the following commands**
    
    * `docker run -d --name <cont-name> -v <vol-name>:<cont-data-path> <image>`
        
    * `docker run -d --name <cont-name> -v <host-dir>:<cont-data-path> <image>`
        
    * `docker run -d --name <cont-name> --mount source=<host-dir>,target=<cont-data-path> <image>`
        
    * **\-v** combines all the options in one field, while the **\--mount** syntax separates them
        
* **Inspecting volumes:-** to see details about a volume use `docker inspect <vol-name>` command.
    
* **List all volumes:-** `docker volume ls`
    
* **Remove named volume:-** `docker volume rm <vol-name>`
    
* **Remove all unused volumes:-** `docker volume prune`
    

### Docker Networking:-

Docker allows you to create virtual spaces called networks, where you can connect multiple containers(small packages that hold all the necessary files for a specific application to run). This way, the containers can communicate with each other and with the host machine(the computer on which docker is installed). Docker's default network is ***bridge*** and it is created with the name ***docker0***.

**Types of Network Drivers:-**

* **Bridge**:- This is a default network driver. This network is commonly used when your application runs in a container that needs to communicate with other containers on the same machine(host). This network creates an isolated network for the containers from the host.
    
* **Host**:- Directly shares the host network with docker. No isolation between the host and the container.
    
* Overlay:- Connects multiple docker containers to communicate across the nodes.
    
* **None**:- Completely isolate a container from the host and other containers. This network is not available for swarm services.
    
* **Macvlan**:- allows us to assign a MAC address to a container, making it appear as a physical device on our network. The Docker-Daemon routes the traffic to containers by their MAC addresses.
    
* **IPvlan**:- This network gives users total control over IPv4 and IPv6 addressing. The VLAN driver builds on top of that giving operators complete control of layer 2 VLAN tagging and even IPvlan L3 routing for users interested in underlay network integration.
    

### Task 1-

### **Create a multi-container docker-compose file that will bring UP and DOWN containers in a single shot ( Example - Create application and database container )**

As we already learn about docker-compose in task 1 of the Day18 challenge, in this blog we'll look at how to increase or decrease the number of replicas for a specific service.

**Note:- We can add *replicas* parameter in the deployment file(.yml) for auto-scaling.**

Add replicas parameter as shown.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1692283414240/ee6d601c-7cce-4e94-8a66-b267c596590f.png align="center")

Note:- Web service has two containers as we specified replicas as 2. Also, ports are given in the range from 8787 to 8791 as we need to scale further.

*Example:-* To increase the number of containers of Web service use `docker-compose up -d --scale web=4`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1692283744038/c88fd79c-1d80-4424-a3a2-b44caa03bce2.png align="center")

This adds two more containers running web service.

*Example:-* To decrease the number of containers of web service use `docker-compose up -d --scale web=1` command.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1692283911924/c6ffbf26-4098-4758-b529-51970f6a01c4.png align="center")

As the web service is running, we can access our website on port *8788*(ref above screenshot).

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1692284118940/b8582952-d581-4905-b92e-b56a53eb620a.png align="center")

You can use the `docker-compose down` command to stop and remove all the containers, networks and volumes associated with the application.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1692284316637/a4e7858c-9384-4ce3-89e9-092df5f6a202.png align="center")

Now you are not able to access the website as it is stopped and removed.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1692284418656/01432b0b-0958-4994-86a7-03c5e64812de.png align="center")

### Task 2-

### **Learn how to use Docker Volumes and Named Volumes to share files and directories between multiple containers.**

Let's create a local volume named My\_Vol1 using the `docker volume create --name My_Vol1` command and inspect the same volume using `docker inspect My_Vol1` command.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1692285731688/8a1e69f5-e79f-4997-a8fd-efcd6999ff7f.png align="center")

Now we'll run two new containers of *nginx* with the same volume attached. Use `docker run -d --name cont1 -p 8181:80 -v My_Vol1:/var/www/html nginx` to run the container.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1692286264541/179bf0d0-3d4d-44ee-a901-0359c7da3255.png align="center")

Now we can create one *index.html* file in the */var/www/html/* directory of *cont1* and see whether it is present in *cont2* or not.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1692288193399/65c16913-ff53-4d6d-a806-a3ff036f3d12.png align="center")

This confirmed that created volume is shared data between two containers and the file created in the cont1 is also accessible to cont2.

To remove the volume first stop all the containers attached to that volume and then remove the volume. I kill the containers as they are no need longer.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1692288723980/5c9d0b74-0618-44a3-9be2-79082b8f5c6c.png align="center")

This is how we use docker volumes.

Thank you so much for taking the time to read till the end! Hope you found this blog informative.

Feel free to explore more of my content, and don't hesitate to reach out if need any assistance from me or in case of you have any questions.

Find me on:- [Hashnode](https://avp23.hashnode.dev/) [LinkedIn](http://www.linkedin.com/in/amit-pawar-7b802b265) [Github](https://github.com/amitvpawar)

**Happy Learning!**
