What is Docker Compose? When and How to use it?

What is Docker Compose? When and How to use it?

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.

docker pull mongo:latest

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

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

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

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

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

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.

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

Docker

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 && 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

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

docker compose ps

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

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

Here’s the screenshot of it.

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 article. I'm happy to hear your feedback and suggestions.

Subscribe to my email newsletter at my site (https://5minslearn.gogosoon.com) which also has a consolidated list of all my blogs.

Cheers!!!