Play Microservices: Integration via docker-compose
Tags: microservices, docker, integration
Published: 2023-07-27 02:48:31

This is the 8th part of a series of articles under the name **"Play Microservices"**. Links to other parts:
Part 1: [Play Microservices: Bird's eye view](https://dev.to/khaledhosseini/play-microservices-birds-eye-view-3d44)
Part 2: [Play Microservices: Authentication](https://dev.to/khaledhosseini/play-microservices-authentication-4di3)
Part 3: [Play Microservices: Scheduler service](https://dev.to/khaledhosseini/play-microservices-scheduler-19km)
Part 4: [Play Microservices: Email service](https://dev.to/khaledhosseini/play-microservices-email-service-1kmc)
Part 5: [Play Microservices: Report service](https://dev.to/khaledhosseini/play-microservices-report-service-4jcm)
Part 6: [Play Microservices: Api-gateway service](https://dev.to/khaledhosseini/play-microservices-api-gateway-service-4a9j)
Part 7: [Play Microservices: client-web service](https://dev.to/khaledhosseini/play-microservices-client-service-4jbf)
Part 8: You are here
Part 9: [Play Microservices: Security](https://dev.to/khaledhosseini/play-microservices-security-45e4)

The source code for the project can be found [here](https://github.com/KhaledHosseini/play-microservices):

---

## Contents:

 - **Summary**
 - **Tools**
 - **Integration via Docker-compose**
 - **To do**

---

### Summary

In the parts 1 to 7, we have developed our microservice application services independently. Those services are:
 - Auth service (plus a database and a cache service)
 - Job Scheduler service (plus a database, a kafka broker and a database for kafka broker)
 - Email job executor service
 - Report service (plus a database, a kafka broker and a database for kafka broker)
 - An api-gateway service which acts as an entry-point to all other services.
 - A client-web service


![services](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xacfaswnpnjacbk3605y.png)

Now it's time to bring everything together. But before, lets have a look at the tools we need.

---

### Tools
The tools required In the host machine:

  - [Docker](https://www.docker.com/): Containerization tool
  - [Git](https://git-scm.com/)

---

### Integration via Docker-compose

If you have followed the previous sessions, you should now have the following folder structure for all our services.


![Folder structure](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/751q2p8wghmacpgp7b1d.PNG)

> - Inside play-microservices folder, create a file named .env and set the contents to:

```shell
AUTH_SERVICE_PORT=9001
AUTH_POSTGRES_PORT=5432
AUTH_REDIS_PORT=6379
ZOOKEEPER_PORT=2181
KAFKA1_PORT=9092
SCHEDULER_MONGODB_PORT=27017
SCHEDULER_SERVICE_PORT=9002
EMAIL_SERVICE_PORT = 9003
POSTFIX_PORT=25
REPORT_SERVICE_PORT = 9004
REPORT_MONGODB_PORT=27018
API_GATEWAY_PORT=5010
CLIENT_PORT = 3000
```
> - This file make it easier for us to handle the ports used by different services.
> - Now, let's create a file called docker-compose.yml inside the play-microservices folder. This file will be used to integrate all the services. The contents of the docker-compose file can be found [here](https://github.com/KhaledHosseini/play-microservices/blob/master/docker-compose.yml).

#### Some notes on the docker-compose.yml file

As depicted in the services diagram, the services exhibit a hierarchical dependency tree. Therefore, inside the docker-compose file, we need to orchestrate the services in such a way that when a service starts, its dependencies are started and running in advance. To achieve this, we use `healthcheck `of docker compose. for example, for auth-db, which is a postgres service, for the `healthcheck `part we have:

```yaml
    healthcheck:
      test: ["CMD-SHELL", "pg_isready"]
      interval: 10s
      timeout: 1s
      retries: 5
```
> - Auth service has dependency to auth-db service. For the `depends_on` condition we have:

```yaml
    depends_on:
      auth-db:
        condition: service_healthy
```
> - Seting condition to `service_healthy` guarantee that auth service will not run unless the auth-db is ready and running.
> - We have other `healthcheck` definitions for zookeeper, kafka and mongodb services. 
> - One other thing to consider in our docker-compose file is the secrets. Each service may have a secret that need to be passed to other services. We use docker-compose secrets capability. When we define a secret for a service, Docker will create a file for that secret inside the container at `/run/secrets/secret-name` path. for example, We have defined a secret for `auth-access-public-key`. We pass this secret to other services that may need authentication service public key for verifying tokens signed by authentication service. 

```yaml
secrets:
  auth-access-public-key:
    file: auth/auth-service/keys/access_token.public.pem
```

> - Open a terminal, cd to play-microservices folder and run `docker-compose up -d --build`. Wait until all services run. Go to `http://localhost:3000/` and voila! Our application is ready to be tested.


![sign up](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t9hasus4oghdqiljr2l9.PNG)


![Image1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e997z0colko26nfu8ly5.png)



![Image 2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iby2avkqt63w7er08k1j.png)

---

### To do

 - deploy to a CI/CD environment via Jenkins
 - Add and run tests