# Day 19 Task: Docker for DevOps Engineers

# Docker-Volume

Docker allows you to create something called volumes. Volumes are like **separate storage areas** that can be accessed by containers. They allow you to store data, like a database, outside the container, so it doesn't get deleted when the container is deleted. You can also mount from the **same volume and create more containers** having the same data.

![How to use volumes to share data between Docker containers | by Edouard  Courty | Medium](https://miro.medium.com/v2/resize:fit:1200/1*xONk464vW-xNYxzE_HsSkw.png align="left")

1. Creating a Docker Volume.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683064444508/53ae7105-fd39-400c-b6f5-2c7752ffa852.png align="center")
    
2. Listing a Docker Volume
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683064471974/7c65fe18-84e1-4fd4-9b79-3810916109ed.png align="center")
    
3. Mounting a Volume
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683064496836/f5a39986-e1fc-48b9-8430-c1e916aca060.png align="center")
    
4. Removing a Volume
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683064536855/455ea1a5-b47b-4442-afd0-64ca639a279e.png align="center")
    
5. Deleting all the volume at once
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683064516266/034059a0-a6f8-4b5b-bae1-94ab3addd8c9.png align="center")
    

# Docker Network

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) together. This way, the **containers can communicate with each other** and with the host machine (the computer on which the Docker is installed). When we run a container, it has its own storage space that is only accessible by that specific container. If we want to share that storage space with other containers, we can't do that.

![How does it work? Docker! Part 2: Swarm networking - OCTO Talks !](https://blog.octo.com/wp-content/uploads/2017/08/bridge-overlay.png align="left")

## Task-1

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

*hints:*

* Use the `docker-compose up` command with the `-d` flag to start a multi-container application in detached mode.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683076981704/7b87b77c-db35-4da6-909c-8ad1b1793ff7.png align="center")
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683077086397/80494894-7845-4213-bbde-cbdb66416937.png align="center")
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683076911103/0f4c95a6-736d-42cb-99eb-51ee8ee89b6c.png align="center")
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683077003414/dba9e562-ac00-447e-8e14-7ae17af82fe6.png align="center")
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683077165472/01e4d0dd-fbfe-4d6b-a794-c5ac08a0aad3.png align="center")
    
* Use the `docker-compose scale` command to increase or decrease the number of replicas for a specific service. You can also add [`replicas`](https://stackoverflow.com/questions/63408708/how-to-scale-from-within-docker-compose-file) in the deployment file for *auto-scaling*.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683080064956/cc334386-0de3-480c-aafe-a4c48b0496d7.png align="center")
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683080019277/3f1a5d5b-1bb5-493c-9b4c-fb99e2679ca4.png align="center")
    
* Use the `docker-compose ps` command to view the status of all containers, and `docker-compose logs` to view the logs of a specific service.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683078610415/599254e5-3c18-4d9f-aec6-f63db37234f7.png align="center")
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683078556372/f25e70e0-0d23-48f9-a54c-0e0b9a997919.png align="center")
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683078633646/bbe3978d-4301-43be-9995-3b86c365cf22.png align="center")
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683078655345/927ffd58-216a-4337-965a-aeb7e1f434ec.png align="center")
    
* Use the `docker-compose down` command to stop and remove all containers, networks, and volumes associated with the application
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683077470975/f4398877-0702-44fd-9f70-d4d89ebd4f31.png align="center")
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683077435557/07d6e0d7-5c04-4711-8c72-4c0c010fd6fc.png align="center")
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683077603024/4b8e69fa-b5a0-46e1-9b46-06397b3102ed.png align="center")
    

## Task-2

* Learn how to use Docker Volumes and Named Volumes to share files and directories between multiple containers.
    
    First creating two folders
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683022710268/25eaf0a8-f5a9-4943-a127-7080846a9212.png align="center")
    
    Sharing the files with docker volume and inspecting it
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683022760982/02a7a3f3-e9d6-4d4f-8bb1-14ad91cd9de1.png align="center")
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683071878767/2c88bc3c-adee-497a-92d7-ecd871ef482a.png align="center")
    
* Create two or more containers that read and write data to the same volume using the `docker run --mount` command.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683022824822/a62849e3-2ced-4b38-a8a4-73e0daf126d3.png align="center")
    
* Verify that the data is the same in all containers by using the docker exec command to run commands inside each container.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683022861123/a68731e6-2e4f-451a-a1bc-65fd518b601c.png align="center")
    
* Use the docker volume ls command to list all volumes and the docker volume rm command to remove the volume when you're done.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683079416563/cdbd19cd-72f4-41a4-bddc-45c5a233351b.png align="center")
    

Happy Learning :)

Mohammed Muqafamuddin
