Support Online
Skip to main content

Guide to Containerize Monorepo with Docker

What will you learn in this guide?

In this guide, you will containerize an application in monorepo structure with Docker.
You will isolate each service and make it ready for deployment locally and on the cloud platform.

🧠 Technical Summary

This guide explains how to containerize multi-service applications with monorepo architecture using Docker.
The goal is to isolate services and provide independent scalable deployment.
Steps: directory structure, Dockerfiles, docker-compose, local testing and cloud deployment.


Problems Encountered in Monorepo Architecture

Monorepo collects all services in a single code repository.
This structure makes development easier but harder to distribute.

Major problems:

  • Services become dependent on each other
  • Different build requirements
  • Local and prod environment mismatch
  • CI/CD processes become more complex

Solution: Container Based Microservice Structure

The most effective solution is to put each service in a Docker container.
This approach provides service isolation and independent scaling.

In this guide:

  • A separate Dockerfile is created for each service
  • local environment is set up with docker-compose
  • Services are deployed independently

How Does Microservice Architecture Work?

  • Requests are directed to the relevant service
  • Services scale independently
  • Logs and monitoring are transferred to central structures

This structure provides high accessibility.


Advantages of This Architecture

  • Isolation: Services do not affect each other
  • Fast CI/CD: Only the changed service is deployed
  • Portability: Consistency across environments is ensured
  • Scalability: Service-based resource management is done
  • Fault Tolerance: If a service crashes, the system remains alive

Step 1️⃣: Creating Monorepo Directory Structure

First, separate the services into separate folders.

mkdir -p services/auth-api
mkdir -p services/frontend
mkdir -p services/todos-api
mkdir -p services/users-api
mkdir -p services/log-processor
mkdir -p services/zipkin
  • This structure allows each service to develop independently.

Step 2️⃣: Adding Dockerfile to Each Service

  1. Each service should have its own Dockerfile. This file defines how to generate the container image.

Example paths:

  1. services/auth-api/Dockerfile

  2. services/frontend/Dockerfile

  3. services/todos-api/Dockerfile

  • This approach ensures consistency in distribution.

Step 3️⃣: Setting Up a Local Environment with docker-compose

  1. Docker-compose is used to run all services with a single command.

version: "3"

services:
frontend:
build: ./frontend
ports:
- "8080:8080"
depends_on:
- auth-api
- todos-api

auth-api:
build: ./auth-api
ports:
- "8081:8081"

todos-api:
build: ./todos-api
ports:
- "8082:8082"

users-api:
build: ./users-api
ports:
- "8083:8083"

zipkin:
image: openzipkin/zipkin
ports:
- "9411:9411"

redis-queue:
image: redis
  • This file ensures that the services start in the correct order.

Step 4️⃣: Local Testing

  1. Build and run all services.

docker-compose up --build
  • This command raises the entire stack.

Access points:

  1. Frontend: http://localhost:8080

  2. Zipkin: http://localhost:9411


Step 5️⃣: Pushing to Git Repository

  1. Upload the project to a Git repository.

git init
git add .
git commit -m "Containerized monorepo"
git remote add origin https://github.com/kullanici/monorepo-app
git push origin main
  • This step is required for cloud deployment.

Step 6️⃣: Deploy to Cloud Platform

  1. Container images are added as separate components for each service.
  • Each service is defined to be independently scalable.

Component types:

1. Web Service: Services that receive HTTP traffic

2. Worker: Background processes

3. Job: Scheduled tasks

  • The platform automatically manages the internal network and DNS.

❓ Frequently Asked Questions (FAQ)

1. Is Docker required for Monorepo? Strongly recommended in prod environment.

2. Can each service use a different language? Yes. Docker provides isolation.

3. Can I make only the frontend public? Yes. Other services may remain private.

4. How to log and monitor? With Zipkin and central log services.


Result

Containerizing the monorepo structure with Docker is the foundation of modern DevOps. This architecture provides rapid deployment and scalability.

You can safely publish all your monorepo and microservice projects on the GenixNode infrastructure 🚀