# Day-18:Docker Compose for DevOps Engineers

# **Docker Compose**

Docker Compose is a tool that allows you to define and manage multi-container Docker applications. It uses a YAML file to define the services, networks, and volumes required for the application, making it easy to spin up and manage complex container-based environments.

For example:

If you have an application that requires an NGINX server and a Redis database, you can create a Docker Compose file that can run both containers as a service without the need to start each one separately.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683527609221/1bf8eb00-260b-4bb1-bb9a-4df7919fe21e.png?auto=compress,format&format=webp&auto=compress,format&format=webp align="left")

## **Working of Docker Compose**

Using Docker-Compose is essentially a three-step process:

1. Define your app’s environment with a `Dockerfile` so it can be reproduced anywhere.
    
2. Define the services that make up your app in `docker-compose.yml` so they can be run together in an isolated environment.
    
3. Run `docker compose up` and the Docker compose command starts and runs your entire app. You can alternatively run `docker-compose up` using Compose standalone(`docker-compose` binary).
    

## **Key features of Docker Compose**

### Have multiple isolated environments on a single host

Compose uses a project name to isolate environments from each other. We can make use of this project name in several different contexts.

The default project name is the basename of the project directory. You can set a custom project name by using the `-p` command line option or the `COMPOSE_PROJECT_NAME` environment variable.

The default project directory is the base directory of the Compose file. A custom value for it can be defined with the `--project-directory` command line option.

### Preserves volume data when containers are created

Compose preserves all volumes used by the services. When `docker compose up` runs, if it finds any containers from previous runs, it copies the volumes from the old container to the new container. This process ensures that any data you’ve created in volumes isn’t lost.

### Only recreate containers that have changed

Compose caches the configuration used to create a container. When you restart a service that has not changed, Compose re-uses the existing containers. Re-using containers means that you can make changes to your environment very quickly.

### Supports variables and moving a composition between environments

Docker Compose supports the use of environment variables to configure your containers at runtime. You can specify environment variables directly in the `docker-compose.yml` file or use an external `.env` file to manage them.

This makes it easy to configure your containers for different environments without modifying the underlying configuration file. Additionally, Docker Compose provides support for managing sensitive data, such as passwords or API keys, using Docker secrets.

## Common use cases of Docker Compose

Docker Compose is widely used for various use cases, especially in scenarios where you need to manage multi-container applications and their dependencies. Here are some common use cases where Docker Compose can be beneficial:

### Development environments

When you’re developing software, the ability to run an application in an isolated environment and interact with it is crucial. The Compose command line tool can be used to create the environment and interact with it.

The **Compose file** provides a way to document and configure all of the application’s service dependencies (databases, queues, caches, web service APIs, etc). Using the Compose command line tool you can create and start one or more containers for each dependency with a single command (`docker compose up`).

### Automated testing environments

An important part of any Continuous Deployment or Continuous Integration process is the automated test suite. Automated end-to-end testing requires an environment in which to run tests.

Compose provides a convenient way to create and destroy isolated testing environments for your test suite.

By defining the full environment in a **Compose file**, you can create and destroy these environments in just a few commands.

### Prototyping and Proof of Concepts

Docker Compose enables rapid prototyping and proof of concepts by allowing one to define and manage the required services in a single configuration file.

It helps in quickly spinning up complex environments with multiple containers, allowing developers and teams to experiment, validate ideas, and iterate faster.

## **Basic Commands in Docker Compose**

<table><tbody><tr><td colspan="1" rowspan="1"><p><strong>Command</strong></p></td><td colspan="1" rowspan="1"><p><strong>Explanation</strong></p></td></tr><tr><td colspan="1" rowspan="1"><p>Docker Compose up</p></td><td colspan="1" rowspan="1"><p>Start all services</p></td></tr><tr><td colspan="1" rowspan="1"><p>Docker Compose down</p></td><td colspan="1" rowspan="1"><p>Stop all services</p></td></tr><tr><td colspan="1" rowspan="1"><p>pip install -U Docker-compose</p></td><td colspan="1" rowspan="1"><p>Install Docker Compose using pip</p></td></tr><tr><td colspan="1" rowspan="1"><p>Docker-compose-v</p></td><td colspan="1" rowspan="1"><p>Check the version of Docker Compose</p></td></tr><tr><td colspan="1" rowspan="1"><p>Docker-compose up -d</p></td><td colspan="1" rowspan="1"><p>Run Docker Compose file</p></td></tr><tr><td colspan="1" rowspan="1"><p>Docker ps</p></td><td colspan="1" rowspan="1"><p>List the entire process</p></td></tr><tr><td colspan="1" rowspan="1"><p>Docker Compose up -d -scale</p></td><td colspan="1" rowspan="1"><p>Scale a service</p></td></tr><tr><td colspan="1" rowspan="1"><p>Docker Compose.yml</p></td><td colspan="1" rowspan="1"><p>Use YAML files to configure application services</p></td></tr></tbody></table>

## **YAML**

YAML stands for YAML Ain't Markup Language

YAML is a human-readable data serialization format used to represent structured data in a simple and easily understandable way. It can be understood as a way to write down information in a format that both humans and computers can read and understand.

YAML is often used for configuration files, data exchange between systems, and defining complex structures. It's commonly used in various programming languages and tools, including **Docker Compose**.

YAML files use a **.yml** or **.yaml** extension.

## **The syntax of the YAML file is:**

```plaintext
keyword: argument
```

## Example of a YAML file:

```plaintext
name: Manoj B
age: 32
email: manojb@gmail.com
```

## **Task-1:- Running Multiple Containers using Docker Compose**

**Step 1:-** First we need to clone the docker image from the docker hub by using the below command

```plaintext
 git clone https:https://github.com/MattsManoj/react_django_demo_app.git
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1688322288209/230f2440-c4bf-4301-b3ec-415a5724dc9c.png align="left")

**Step 2:-** Now we need to use docker-compose in the Linux machine, By the below command

```plaintext
sudo apt-get install docker-compose -y
```

**Step 3:-** Using the Vim editor we need to create a docker-compose file

```plaintext
version: '3.9'

services:
  web:
    image: mattsmanoj/react_django_app:latest
    ports:
      - "8001:8001"
```

**Step 4:-** Now we need to start the container using the below command

```plaintext
sudo docker-compose up -d
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1688405307072/26bb44b3-6c38-4556-9afa-34d26f7b86a4.png align="left")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1688405344199/d48452b3-2e28-48f8-8b62-05614ed7a6d5.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1688405357508/04215dda-fc30-4a52-a1de-6eee7c65e789.png align="center")

**Step 5:-** Now we need to search in the web browser along with the ipv4 address and port number

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1688405383731/61b5150d-ffe9-4ce9-8db0-9954c51c941b.png align="left")

**Step 6:-** If we need to down or close the application then need to use the below command

```plaintext
sudo docker-compose down
```

## **Task 2:-**

1. Pull a pre-existing Docker image from a public repository (e.g. Docker Hub) and run it on your local machine.
    
2. [**Run**](http://2.run/) the container as a non-root user (Hint- Use `usermod` command to give the user permission to docker).
    
3. Make sure you reboot the instance after giving permission to user. - Inspect the container's running processes and exposed ports using the docker inspect command.
    
4. Use the docker logs command to view the container's log output.
    
5. Use the docker stop and docker start commands to stop and start the container.
    
6. Use the docker rm command to remove the container when you're done.
    
    **Pull a pre-existing Docker image from a public repository (e.g. Docker Hub) and run it on your local machine**
    
    1. I am pulling the Nginx image. and running the container.
        
        ```plaintext
         docker pull nginx
         docker run -d -p 8080:80 nginx
        ```
        

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1688489505847/1429895d-370a-4fff-981e-d71a8ee8897b.png align="left")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1688489519930/e88fc51c-b35e-42ad-945b-443c89b73309.png align="left")

**Run the container as a non-root user (Hint- Use** `usermod` command to give the user permission to docker). Make sure you reboot the instance after permitting the user.

To run the container as a non-root user and use the docker commands without sudo, we need to give the user permission to docker using the following command and then reboot the system:

```plaintext
    sudo usermod -aG docker ubuntu
    sudo reboot
```

### Inspect the container's running processes and exposed ports using the docker inspect command

The `docker inspect` command is used to retrieve detailed information about Docker objects such as containers, images, networks, and volumes.

```plaintext
    docker images
    docker inspect nginx
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1688489534064/b477637a-14c9-45bc-bd2f-e868e7b7a1bd.png align="left")

The output of docker inspect command has various details in JSON format. Look for the following sections to use custom formatting and inspect running processes and exposed ports:

* `"State"` section: This section provides information about the container's current state, including whether it is running or stopped.
    
* `"NetworkSettings"` section: Here, you can find details about the container's network configuration, including the IP address and exposed ports.
    
* ```plaintext
            docker inspect --format='{{json .State }}' 5519f6a83cb8
    ```
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1688489955377/47dd34eb-4caf-49f9-96ba-f3f810040546.png align="center")

**Use the docker logs command to view the container's log output.**

```plaintext
    docker logs <container_id>
    docker logs 5519f6a83cb8
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1688490093541/2aaa3579-5730-4038-9cd1-db71059b5837.png align="center")

**Use the docker stop command to stop the container.**

```plaintext
    docker stop cont_id

    docker stop 5519f6a83cb8
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1688490647952/d196d5d4-47ae-42cc-a3bd-ad918b65355a.png align="center")

**Use the docker start command to start the container.**

```plaintext
    docker start cont_id

    docker start 5519f6a83cb8
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1688490688822/0f086a3d-2bf6-40b9-ae20-f4b35966d34d.png align="center")

**Use the docker rm command to remove the container when you're done.**

We cannot remove a running container. Hence, either we need to stop the container and then delete the container or perform the forceful removal of the container. Let's forcefully remove the container:

```plaintext
    docker rm -f container_id
    docker rm -f 5519f6a83cb8
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1688491011133/636e1ca8-88b6-496d-b742-25a1f31151ef.png align="center")

This will remove the `nginx` container from your local machine

*Thank you for reading!!* Hope you find this helpful.*Thank you for reading!!* Hope you find this helpful.

#day18challenge#90daysofdevops

Always open to suggestions..!!

~ Manoj Bhamidipati 🙂
