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

Table of contents
- π’ What is Docker Compose?
- π οΈ Why Use Docker Compose?
- π¦ Basic Syntax of a docker-compose.yml File
- π Line-by-Line Explanation
- βοΈ How to Use Docker Compose
- π Benefits of Docker Compose in Deployment
- π Real-World Use Case
- 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
- π Step 2: Write a Docker Compose File
- π Step 3: Connect to Your Virtual Machine
- π€ Step 4: Transfer Files to the VM
- π Step 5: Deploy Using Docker Compose
- π§Ή Step 6: Clean Up and Restart
π’ 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:
Subscribe to my newsletter
Read articles from Lav kushwaha directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
