3rd Week :- What is Docker Compose? A Complete Beginner's Guide

Lav kushwahaLav kushwaha
4 min read

🚒 What is Docker Compose?

Docker Compose is a tool that simplifies running multi-container Docker applications. Instead of starting each container manually using docker run, Docker Compose lets you define and run multi-container applications with a single command using a file called docker-compose.yml.

It is especially useful in development and deployment environments where your application depends on multiple services like databases, caches, or backend/frontend servers.


πŸ› οΈ Why Use Docker Compose?

  • Simplified configuration: All services are defined in one YAML file.

  • Easy environment replication: Consistency across dev, staging, and production.

  • One-command startup: Run docker-compose up and all services start together.

  • Networking handled automatically: Containers can communicate with each other via service names.


πŸ“¦ Basic Syntax of a docker-compose.yml File

Here’s a simple example of a docker-compose.yml for a Node.js app with a MongoDB database:

version: '3.8'
services:
  app:
    build: .
    ports:
      - "3000:3000"
    depends_on:
      - mongo
    environment:
      - MONGO_URL=mongodb://mongo:27017/mydb

  mongo:
    image: mongo:latest
    ports:
      - "27017:27017"
    volumes:
      - mongo-data:/data/db

volumes:
  mongo-data:

πŸ” Line-by-Line Explanation

version: '3.8'

Defines the version of the Docker Compose file format. Version 3.8 is compatible with the latest Docker features.

services:

Declares the services (containers) to be managed. Each service is a container or group of containers.

app:

Our main Node.js application service.

  • build: . – Tells Docker to build the image from the Dockerfile in the current directory.

  • ports: – Maps port 3000 on the host to port 3000 in the container.

  • depends_on: – Ensures the MongoDB container starts before the app.

  • environment: – Sets environment variables used in the app (e.g., MongoDB URL).

mongo:

The MongoDB service.

  • image: – Specifies the official MongoDB image from Docker Hub.

  • ports: – Exposes port 27017.

  • volumes: – Persists database data using named volumes.

volumes:

Defines the named volume mongo-data used by the MongoDB service to store data persistently.

βš™οΈ How to Use Docker Compose

1. Create a Dockerfile (for your app)

# Dockerfile
FROM node:18
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["node", "index.js"]

2. Create docker-compose.yml (as above)

3. Run Your App with Compose

docker-compose up --build

This builds the image (if not already built), creates containers, and starts all services.

To stop:

docker-compose down

πŸš€ Benefits of Docker Compose in Deployment

  • Single command deployment: Easily spin up all services with one command.

  • Environment consistency: Run the same setup locally, in staging, or in production.

  • Isolation: Each container runs in its own environment, avoiding dependency conflicts.

  • Scalability: Easily scale services using docker-compose up --scale app=3.

  • Easy version control: YAML files can be stored in your repo and versioned.


πŸ“˜ Real-World Use Case

Imagine you're building a MERN app (MongoDB, Express, React, Node). Instead of setting up MongoDB manually and worrying about service communication, Docker Compose handles it all.

With one command, it:

  • Builds and starts the Node backend

  • Starts MongoDB

  • Connects them via a private network

You can also add Redis, Nginx, or any other services in the same YAML file.


Deploying a Monorepo to VMs using Docker Compose

Monorepos are repositories that host multiple applications/services (e.g., frontend, backend, database) in a single codebase. Docker Compose makes deploying such structures onto VMs efficient and reliable.

🧾 Step 1: Structure Your Monorepo

Organize your repo like this:

my-monorepo/
β”œβ”€β”€ backend/
β”‚   β”œβ”€β”€ Dockerfile
β”‚   └── ...
β”œβ”€β”€ frontend/
β”‚   β”œβ”€β”€ Dockerfile
β”‚   └── ...
β”œβ”€β”€ docker-compose.yml
└── .env

πŸ›  Step 2: Write a Docker Compose File

version: '3.8'
services:
  frontend:
    build: ./frontend
    ports:
      - "3000:3000"
    depends_on:
      - backend

  backend:
    build: ./backend
    ports:
      - "5000:5000"
    environment:
      - DATABASE_URL=mongodb://mongo:27017/mydb
    depends_on:
      - mongo

  mongo:
    image: mongo:latest
    ports:
      - "27017:27017"
    volumes:
      - mongo-data:/data/db

volumes:
  mongo-data:

🌍 Step 3: Connect to Your Virtual Machine

Use SSH to access your VM:

ssh user@your-vm-ip

Make sure Docker and Docker Compose are installed on the VM.

πŸ“€ Step 4: Transfer Files to the VM

You can use scp or Git:

scp -r my-monorepo user@your-vm-ip:/home/user/

Or clone the repo:

git clone https://github.com/yourusername/my-monorepo.git

πŸš€ Step 5: Deploy Using Docker Compose

Navigate to the directory and run:

cd my-monorepo
docker-compose up -d --build

This builds the containers, sets up networking, and starts all services in the background.

🧹 Step 6: Clean Up and Restart

To stop services:

docker-compose down

To rebuild and restart:

10
Subscribe to my newsletter

Read articles from Lav kushwaha directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Lav kushwaha
Lav kushwaha