# Day 19 Task: Docker for DevOps Engineers

### *<mark>Docker-Volume:</mark>*

* Docker volumes are like special folders 📁 that allow containers to store and share data.
    
* They solve the problem of data loss when containers are stopped or removed 🚫🗑️.
    

🆚 Why are they needed?

* Containers are like magic boxes 🪄 that disappear when closed 🚪.
    
* So, any data saved inside them is gone forever when the container is shut down.
    

💾 Types of Docker Volumes:

1. **Named Volumes** 📛:
    
    * These are managed by Docker 🐳.
        
    * Have easy-to-remember names 🏷️.
        
    * Data stays safe even if containers using them are removed.
        
2. **Bind Mounts** 🔄:
    
    * These are folders from your computer 🏠.
        
    * You choose where to link them 🗂️.
        
    * Changes are visible both inside the container and on your computer 🔄.
        

🔧 How to Use Docker Volumes:

* **Create a Named Volume** 📝:
    
    * Command: `docker volume create my_volume`
        

***<mark>Benefits of Docker Volumes:</mark>***

1. **Data Persistence** 📝:
    
    * Volumes ensure that data inside containers is not lost when containers are stopped or removed.
        
    * Data stored in volumes remains available even if the container is restarted or replaced.
        
2. **Data Sharing** 🔄:
    
    * Volumes enable easy data sharing between containers.
        
    * Multiple containers can use the same volume to access and modify shared data.
        
3. **Isolation** 🚧:
    
    * Volumes keep data separate from the container's file system.
        
    * This isolation prevents accidental data corruption or interference between containers.
        
4. **Backup and Restore** 💾:
    
    * Docker volumes make it simple to back up and restore data.
        
    * You can easily create snapshots of volumes, facilitating data recovery.
        

# <mark>Docker Network:</mark>

Docker networks are a way to enable communication and connectivity between Docker containers. When containers need to interact with each other, or with the outside world, they can do so using Docker networks. These networks act as virtual "lanes" that allow containers to communicate securely and efficiently.

* **Container Isolation**: Containers are isolated by default, and without a network, they cannot communicate with each other.
    
* **Microservices Architecture**: In modern application design, applications are often split into microservices, and these services need to communicate with each other over the network.
    
* **External Connectivity**: Containers often need to connect to the internet or other external services, and networks provide a secure and controlled connection.
    

## <mark>Task-1</mark>

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690906045588/9361c4d0-779a-4112-ba25-c3d6e2f079cf.png align="center")

***<mark>We have to modify the docker-compose file:</mark>***

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690909950282/f707e0a3-2f44-4c4e-a12d-48f3da6131a2.png align="center")

**<mark>Use the </mark>** `docker-compose up` **<mark>command with the </mark>** `-d` **<mark>flag to start a multi-container application in detached mode.</mark>**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690955313860/d8386ca1-5490-4770-8175-df38da5066df.png align="center")

***<mark>Use the </mark>*** `docker-compose scale` ***<mark> command to increase or decrease the number of replicas for a specific service. You can also add </mark>*** [`replicas`](https://stackoverflow.com/questions/63408708/how-to-scale-from-within-docker-compose-file) ***<mark> in the deployment file for auto-scaling.</mark>***

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690955730617/a7521f62-eefc-439a-8729-5678a1800f7e.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690955756310/04dbcf8c-d2d6-44a0-92ad-10053960c425.png align="center")

**<mark>Use the</mark>** `docker-compose ps` **<mark>command to view the status of all containers, and</mark>** `docker-compose logs` **<mark>to view the logs of a specific service.</mark>**

docker-compose logs

**<mark>Use the</mark>** `docker-compose down` **<mark>command to stop and remove all containers, networks, and volumes associated with the application:</mark>**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690956164706/caf931b1-4ac2-454d-82b4-8ccb7ede25f9.png align="center")

* ### **<mark>Task-2:</mark>**
    
    **Learn how to use Docker Volumes and Named Volumes to share files and directories between multiple containers**
    
* First, create a volume.
    
    **$docker volume create my-vol**
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690956303124/bc3ff69d-0ab5-430b-ba73-223c5cc3d193.png align="center")

<mark>Now we need to create a different container using the same volume:</mark>

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690956693612/9a08a74d-c3ee-4018-b07b-eaa4b32c912e.png align="center")

**<mark>Verify that the data is the same in all containers by using the docker exec command to run commands inside each container.</mark>**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690957423811/e24b29b4-d1bf-4c22-9442-e3a45270b863.png align="center")

**<mark>Create two or more containers that read and write data to the same volume using the docker run --mount command.</mark>**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690959511231/5ed11845-35fa-4ef4-b049-4afaf3ad3263.png align="center")

<mark>Now we are creating a container using --mount and the same volume:</mark>

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690959785362/16ea75e7-0361-40c8-bf8d-dd127e265c63.png align="center")

<mark>Now we need to go to the myapp container and need to create a text file and exit:</mark>

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690959884669/904a7149-d0ef-4d7a-93e5-6550472d2b83.png align="center")

**<mark>Use the docker volume ls command to list all volumes and the docker volume rm command to remove the volume when you're done.</mark>**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690960218635/dc52fb6d-1a6d-48de-9aba-4f8123ea59cf.png align="center")

Thanks for reading.
