# 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 same data.

# 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.

## 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 )

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

### <mark>Creation two-tier flask application</mark>

create a new directory

a)mkdir projects

b)cd projects

c)Clone the GitHub repository in your local.

d)git clone [https://github.com/LondheShubham153/two-tier-flask-app.git](https://github.com/LondheShubham153/two-tier-flask-app.git)

e)cd two-tier-flask-app

### <mark>First, you have to install docker-compose and then execute the command.</mark>

## [Step 1 — Installing Docker Compose](https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-compose-on-ubuntu-20-04#step-1-installing-docker-compose)

To make sure you obtain the most updated stable version of Docker Compose, you’ll download this software from its [official Github repository](https://github.com/docker/compose).

First, confirm the latest version available in their [releases page](https://github.com/docker/compose/releases). At the time of this writing, the most current stable version is `1.29.2`.

The following command will download the `1.29.2` release and save the executable file at `/usr/local/bin/docker-compose`, which will make this software globally accessible as `docker-compose`:

<mark>Command</mark>\=The following command will download the `1.29.2` release and save the executable file at `/usr/local/bin/docker-compose`, which will make this software globally accessible as `docker-compose`:

```plaintext
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
```

Next, set the correct permissions so that the `docker-compose` command is executable:

```plaintext
sudo chmod +x /usr/local/bin/docker-compose
```

To verify that the installation was successful, you can run:

```plaintext
docker-compose --version
```

### **<mark>Run the docker-compose command</mark>**

docker-compose up -d

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1702663129719/e968732c-becb-492b-9c20-9e15a145c683.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1702663178649/047b8885-a900-435a-b59f-107398678a73.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1702663216568/d55cbac2-02df-48bc-a325-208afab43e68.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1702663233851/c6efb6fa-9592-4e41-b02f-45779a931ec7.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1702663254469/25072768-6893-4562-b9a2-741cdbe1646a.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1702663508313/c13e8014-ab8c-4c1e-8067-02fd9112ec78.png align="center")

### 2.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 deployment file for *auto-scaling*.

the `docker-compose scale` command allows you to change the number of containers for a particular service defined in your `docker-compose.yml` file. However, keep in mind that `docker-compose` doesn't support auto-scaling natively; you'd need to manually adjust the number of replicas. Here's how you'd use it:

Let's say you have a service named `web` defined in your `docker-compose.yml`:

```yaml
version: '3'
services:
  web:
    image: nginx:latest
    ports:
      - "80:80"
```

To scale the `web` service to 3 containers, you'd use the `docker-compose scale` command:

```bash
docker-compose up -d  # Start the initial service
docker-compose scale web=3  # Scale the 'web' service to 3 containers
```

This will create two additional containers based on the `nginx:latest` image, effectively having three instances of the `web` service running.

### 3.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.

<mark>Command name</mark>\=`docker-compose ps`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1702664257152/b718d6b7-fb6b-4d34-af5a-f3b161ed230f.png align="center")

<mark>docker-compose logs</mark>

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1702664332788/13f541b6-b8e5-43cb-aa4b-2c830a8d2dac.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1702664344200/c983fa5a-0f4f-49fb-8cba-32d7e7995f19.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1702664358584/8efa76aa-34d2-4926-a984-7cabfb4f58c2.png align="center")

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

<mark>docker-compose down</mark>

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1702664492148/fd2888be-9e1b-4d08-b9db-70a464450e3c.png align="center")

# **Task-2**

### **1.<mark>Learn how to use Docker Volumes and Named Volumes to share files and directories between multiple containers.</mark>**

Docker Volumes and Named Volumes are powerful features for sharing files and directories between containers or persisting data even after containers are stopped or removed. Here's a breakdown of how to use them:

### Docker Volumes:

#### 1\. Creating a Volume:

You can create a Docker volume using the `docker volume create` command:

```bash
docker volume create my_volume
```

#### 2\. Attaching a Volume to a Container:

To use this volume in a container, you specify it when running the container:

```bash
docker run -d -v my_volume:/path/in/container my_image
```

This command will run a container using `my_image` and mount the `my_volume` volume to the specified path inside the container (`/path/in/container`).

#### 3\. Mounting Host Directories as Volumes:

You can also mount a directory from your host machine to a container:

```bash
docker run -d -v /host/path:/container/path my_image
```

Here, `/host/path` is a directory on your host machine, and `/container/path` is the path where it will be mounted inside the container.

### Named Volumes:

Named volumes are a specific type of Docker volume that allows easier management and reference by a user-defined name instead of a random string assigned by Docker.

#### 1\. Creating a Named Volume:

```bash
docker volume create my_named_volume
```

#### 2\. Using Named Volumes in Containers:

Similar to the previous method, you can use named volumes when running a container:

```bash
docker run -d -v my_named_volume:/path/in/container my_image
```

#### 3\. Inspecting Named Volumes:

To see information about a named volume:

```bash
docker volume inspect my_named_volume
```

#### Example Use Case:

Let's say you have two containers: `app` and `database`, and you want to share data between them. You could use a named volume to share a directory where the database stores its data:

```bash
docker volume create db_data
docker run -d -v db_data:/var/lib/database database_image
docker run -d -v db_data:/app/database_data app_image
```

Both containers (`database_image` and `app_image`) will now share the `db_data` named volume, allowing them to access and modify data in the `/var/lib/database` directory from within their respective containers.

Volumes and named volumes provide flexible ways to manage data and share files between containers in Docker, offering durability and persistence even after containers are stopped or removed.

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

Using the `docker run --mount` command, you can create multiple containers that read from and write to the same volume. Let's create two containers that read and write data to a shared volume:

### 1\. Create a Docker Volume:

First, let's create a volume named `shared_volume`:

```bash
docker volume create shared_volume
```

### 2\. Start Containers with Shared Volume:

#### Container 1:

Run the first container and mount the `shared_volume` to a specific path inside the container (`/data` in this example):

```bash
docker run -d --name container1 --mount source=shared_volume,target=/data my_image1
```

Replace `my_image1` with the image you want to use for the first container.

#### Container 2:

Similarly, run the second container and mount the same `shared_volume` to a different path:

```bash
docker run -d --name container2 --mount source=shared_volume,target=/data my_image2
```

Replace `my_image2` with the image you want to use for the second container.

### Data Interaction:

Now, both `container1` and `container2` have access to the same volume mounted at `/data`.

Any changes made by `container1` in the `/data` directory will be reflected in `container2`, and vice versa. They can read, write, and modify files within this shared volume.

For instance, within each container, you can interact with the shared volume by creating, modifying, or deleting files within the `/data` directory:

```bash
# Inside container1
echo "Data from container1" > /data/file_from_container1.txt

# Inside container2
cat /data/file_from_container1.txt  # This will display "Data from container1"

# Inside container2
echo "Data from container2" > /data/file_from_container2.txt

# Inside container1
cat /data/file_from_container2.txt  # This will display "Data from container2"
```

Both containers will have access to the changes made within the shared volume, allowing them to read and write data interchangeably.

Remember to replace `my_image1` and `my_image2` with the actual images you want to use for these containers and adjust the paths and commands based on your specific use case and requirements.

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

You can verify that the data remains consistent across all containers sharing the same volume by using the `docker exec` command to run commands inside each container.

Let's assume you have two containers, `container1` and `container2`, both sharing the same volume named `shared_volume`. To verify the data consistency:

### 1\. Access `container1` and create a file inside the shared volume:

```bash
docker exec -it container1 bash  # Access container1's shell
echo "Data from container1" > /data/file_from_container1.txt  # Create a file
exit  # Exit container1's shell
```

### 2\. Access `container2` and check if it can read the file created by `container1`:

```bash
docker exec -it container2 bash  # Access container2's shell
cat /data/file_from_container1.txt  # Check content of the file
exit  # Exit container2's shell
```

By running these commands, you can confirm that `container2` is able to read the file `file_from_container1.txt` created by `container1`. This demonstrates that the data is indeed shared and consistent across both containers due to their common access to the shared volume `shared_volume`.

You can perform additional actions like modifying files, creating directories, or deleting content within the shared volume from different containers using `docker exec` to verify that all changes are reflected consistently across all containers sharing that volume.

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

The `docker volume ls` command allows you to list all Docker volumes on your system, while `docker volume rm` is used to remove a specified volume.

### List All Volumes:

To list all volumes available on your system, use the following command:

```bash
docker volume ls
```

This command will display a list of all Docker volumes along with their names and other details.

### Remove a Volume:

To remove a specific volume, you'll use the `docker volume rm` command followed by the name of the volume you want to delete. For example, to remove a volume named `shared_volume`:

```bash
docker volume rm shared_volume
```

Replace `shared_volume` with the actual name of the volume you want to remove.

Remember, make sure the volume is not being used by any running containers before attempting to remove it. If a volume is in use, you might need to stop or remove the containers associated with it before you can delete the volume.

Always be cautious when removing volumes as it will also remove all data stored within that volume permanently.

After you're done and have confirmed that the volume is no longer needed, you can use `docker volume rm` to remove it to free up space or for cleanup purposes.

### ***Thank you for reading this Blog. Hope you learned something new today! If you found this blog helpful, please like, share, and follow me for more blog posts like this in the future😊😊***
