What is Docker Compose? When and How to use it?
Tags: docker, compose, javascript, mongodb
Published: 2023-05-24 03:30:00

Docker helps you to setup development environment on your machine quickly. It hardly takes a couple of minutes for the entire process to complete. But let’s assume you were assigned on a project which requires at least 10 different services in running state to run your project. Assuming your project requires Java 8, Node 14, MySQL, MongoDB, Ruby on rails, RabbitMQ, etc. Ideally this may not be the case, but it’s just an assumption. In such case, you have to pull all those images individually from Docker and start all of them in their containers. At some point, one process may depend on another to run. So, you have to order them. It would be good if it’s a one time process. But, not just once, every day, every time you start working on your project, you have to start all these services.

That’s a tedious process right?

To overcome this Docker introduced a concept called Multi Containers (Docker Compose). Before learning about docker compose, let’s quickly learn how to start a database host in docker. In the example part of this tutorial, we’ll spin up a NodeJS container and MongoDB container together. Learning about MongoDB at the beginning will give you a good understanding when we move to docker compose.

Let’s split this tutorial into 2 sections.

1. How to use docker as a Database host (MongoDB)
2. Docker compose with an example (NodeJS & MongoDB)

## How to use docker as a Database host
If you had experience on backend development, you might have a chance to handle multiple database. Databases like MySQL/Postres to handle relational data and Cassandra/MongoDB to handle unstructured data.

Do you know a secret, We can work on Back-end development without installing the database on your machine locally. Yes, we can use the docker as a database host. It has all the dependencies by default in the particular image file.

## Why do we need to use Database in Docker instead of Local?
As I’ve already mentioned in my old blog, Docker helps us maintain consistent versions across platforms & environments. Let’s consider there are a group of people working in your team on MongoDB version 5.0. If a new techie joins your team, he/she needs to set up the same version with the exact configuration manually. What if they install the latest version of MongoDB (6.0) which will lead to some conflict. This will be nightmare if it spreads to everyone’s device.

To overcome this, We can use the MongoDB in docker with a custom configuration and push the MongoDB Image to the docker hub internally. If a new person comes in they can pull the image and start the implementation without any manual configuration.

Let’s quickly get into the advantages of using a database in docker.

1. By using this implementation we can ensure that everyone in a team uses the exact runtimes and configuration without any external resources.
2. It’s very easy to set up and we can start/stop the server quickly using the docker desktop

## How to setup MongoDB using Docker
Hope you’re aware of Docker Hub. If not, here’s a short intro. Docker hub is a platform where we can find, and share docker images in public or private. It is more similar to GitHub / GitLab. In a nutshell, it’s a repository for docker images.

The first step is to pull the official docker image from the docker hub.

![MongoDB image in Docker Hub](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vbrw8hkbpcryzm6o6e6v.png)

```
docker pull mongo:latest
```

![Pull MongoDB image from Docker](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g80cqgrepxq0aouzg3ex.png)

Once you’re done pulling the mongo image, open your Docker Desktop and you’ll be able to see it there.

![Docker Desktop Dashboard](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rlwdkpka7lr29ylyovk2.png)

Let’s run our MongoDB image using the docker run command.

```
docker run -d -p 27017:27017 --name mongo-server-local mongo: latest
```

![Run the docker image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ilbqdq0wvkuhxbipednu.png)

We successfully run the docker image. Now we can see the container running on the docker desktop.

![Docker Desktop Container Dashboard](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n7aeu6se4dsolq6nr2y9.png)

So, the MongoDB server is running on your machine. Let’s ensure this in the browser. Hit [http://localhost:27017](http://localhost:27017) on your browser and you should be able to see the message as shown in the screenshot.

![Output from MongoDB when accessing via HTTP](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/av4isem4t8bty1f6ui8n.png)

Interesting right?

We can stop/start the MongoDB server using docker whenever we need.

### Important Note

1. It is not recommended to use the docker as a database for production
2. Do not use docker database for large scale applications

## What is docker-compose
Let’s come back to docker-compose.

Docker Compose is a tool used to define and share multi-container applications. This means we can run a project with multiple containers using a single source.

For example, assume you’re building a project with NodeJS and MongoDB together. We can create a single image that starts both containers as a service, you don’t need to start each separately.

Interesting right? And this solves the problem which I called out at the very beginning of this article.

To achieve this we need to define a `docker-compose.yml`.

#### docker-compose.yml

The compose file is a YML file defining services, networks, and volumes for a docker container. There are several versions of the Compose file format available – 1, 2, 2.x, and 3.x.

Before proceeding further, here’s a important note to us from the [Docker Compose team](https://docs.docker.com/compose/).

> From the end of June 2023 Compose V1 won’t be supported anymore and will be removed from all Docker Desktop versions.

We are using version 3 in this article.

```
version: '3'
services:
  app:
    image: node:latest
    container_name: app_main
    restart: always
    command: sh -c "yarn install &amp;&amp; yarn start"
    ports:
      - 8000:8000
    working_dir: /app
    volumes:
      - ./:/app
    environment:
      MYSQL_HOST: localhost
      MYSQL_USER: root
      MYSQL_PASSWORD:
      MYSQL_DB: test
  mongo:
    image: mongo
    container_name: app_mongo
    restart: always
    ports:
      - 27017:27017
    volumes:
      - ~/mongo:/data/db
volumes:
  mongodb:
```

Let’s dismantle the above code and understand one by one.

`version` – Refers to the docker-compose version (Latest 3)

`services` – Define the services that we need to run

`app` – Custom name for one of your containers

`image` – Image which we have to pull. Here we are using node:latest and mongo.

`container_name` – Name for each container

`restart` – Start/Restart a service container

`port` – Define the custom port to run the container

`working_dir` – The current working directory for the service container

`environment` – Define the environment variables, such as DB credentials, etc.

`command` – Command to run the service

## How to run the multi-container?
We need to build it using docker build.

```
docker compose build
```

After successfully building, We can run the containers using up Command.
```
docker compose up
```

If you want to run the container in detached mode just use `-d` flag.
```
docker compose up -d
```

![Run all images in a single compose command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k33rfn5dcfiiah80jtr9.png)

Fine, We are good to go. The containers are up and running. Let’s check the container list.
```
docker compose ps
```

![Status of running docker containers](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2ec5j28mfmhzlbg21hvb.png)

Hurray, We can see that there are two containers running at the same time.

![Testing NodeJS](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1qwxoxizaish1gb8zmkr.png)


![Testing MongoDB](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/66ok283kmejq64uume0u.png)

To have a look at the data in your MongoDB, you have to install MongoDB Compass.

Here’s the screenshot of it.

![MongoDB Compass](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/76zubvqkk9wvua1ny4ml.png)

## Conclusion
In this article, you have learnt about Docker Compose with an example. Using this multiple containers you can spin up any type of service such as RabbitMQ, Apache Kafka and run in a single source of service. Hope you enjoyed reading this article. 

Hit `Like` if you like the blog and comment your feedback and suggestions. 

If you wish to learn more about Docker, subscribe to my article at my [site](https://5minslearn.gogosoon.com/?ref=dev_to_docker_compose) ([https://5minslearn.gogosoon.com](https://5minslearn.gogosoon.com/?ref=dev_to_docker_compose)). It also has a consolidated list of all my blogs. 

Cheers!!!