# Day 19:Docker for DevOps Engineers

# **Docker-Volume**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683786975297/889c403c-e339-4ef3-825a-786cdd48a0e5.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683787053308/6d18728a-0d41-428b-af24-9e4b60c08bf6.png align="center")

# **Docker Network**

* Docker Network is a feature that enables different containers to communicate with each other and with other networks. Docker provides several built-in network drivers that allow containers to connect to the host network. You can also create custom network drivers to meet specific network requirements.
    
* **some of the most commonly used Docker network drivers:**
    
    * `bridge`: The default network driver. If you don’t specify a driver, this is the type of network you are creating. **Bridge networks are usually used when your applications run in standalone containers that need to communicate.**
        
    * `host`: For standalone containers, remove network isolation between the container and the Docker host, and use the host’s networking directly.
        
    * `overlay`: Overlay networks connect multiple Docker daemons and enable swarm services to communicate with each other. You can also use overlay networks to facilitate communication between a swarm service and a standalone container, or between two standalone containers on different Docker daemons. This strategy removes the need to do OS-level routing between these containers.
        
    * `macvlan`: Macvlan networks allow you to assign a MAC address to a container, making it appear as a physical device on your network. The Docker daemon routes traffic to containers by their MAC addresses. Using the `macvlan` driver is sometimes the best choice when dealing with legacy applications that expect to be directly connected to the physical network, rather than routed through the Docker host’s network stack.
        
    * `none`: For this container, disable all networking. Usually used in conjunction with a custom network driver. `none` is available for swarm services.
        
    
    ## **Task-1**
    
    Create a multi-container docker-compose file that will bring *UP* and bring *DOWN* containers in a single shot
    

Write the following content in **vi docker-compose.yml**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683785069195/c209a157-8d6f-426e-b698-295b4ebc2fc6.png align="left")

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

* ```plaintext
    docker-compose up -d
    ```
    

* ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683787686512/f16e6fc9-7e00-450b-81a6-bf95a693d578.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*.
    
* ```plaintext
    docker-compose scale flask-api=3
    ```
    
* The above commands will start the services defined in the YAML file in detached mode and then scale the `flask-api` service to three instances. Docker Compose will automatically load and balance incoming requests between the running instances.
    
* 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.
    

```plaintext
docker ps
  docker-compose logs
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683787796710/94edaf8b-68ae-43e4-8b94-5b1754d18292.png align="center")

Use the `docker-compose down` command to stop and remove all containers, networks, and volumes associated with the application

```plaintext
 docker-compose down
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683787863762/2a7d6bca-2cf5-4e4b-bd30-700caf76adc0.png align="center")

## **Task-2**

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

**Docker Volumes** are a way to store data in a container that can be shared with other containers. You can create a Docker Volume using the `docker volume create` command.

example:

```plaintext
 docker volume create flaskapp
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683787971412/3bd7c972-b89d-4962-8ce5-f6105cdd03a2.png align="center")

* This will create a new volume that can be mounted to a container using the `--mount` or `-v` option.
    
* Named Volumes:
    
    Named Volumes are similar to Docker Volumes, but they have a name that can be used to reference them. You can create a Named Volume using the `docker volume create` command with the `--name` option.
    
    Example:
    

```plaintext
docker volume create --name my_name_volume
```

Create two or more containers that read and write data to the same volume using the `docker run --mount` command.

```plaintext
 docker run -d --name container2 --mount source=my_name_volume,target=/data alpine sleep 3600
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683788211269/1efd9814-0ac4-4277-a28c-aee19c25a16a.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.
    
* ```plaintext
     # Write a file in container1
      docker exec container1 sh -c 'echo "Hello World" > /data/file.txt'
    
      # Read the file in container2
      docker exec container2 cat /data/file.txt
    ```
    
    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/v1683788373532/44f168ac-6472-461b-b61b-b4a3ad290ee1.png align="center")
