๐Ÿงฉ (Post 3): Docker Compose in Action: Building Multi-Container Apps!

In real-world applications, it's rare to run a single container. You usually have a web app, a database, maybe a caching layer or a reverse proxy. That's where Docker Compose comes in โ€” your best friend for orchestrating multi-container systems with ease.

๐Ÿ“˜ What is Docker Compose?

Docker Compose is a tool that lets you define and manage multi-container Docker applications using a single YAML file. With one command, you can bring up or tear down an entire environment.

Benefits:

๐Ÿงพ Writing a docker-compose.yml

Let's build a stack with:

๐Ÿ›  Folder structure:

project/
โ”œโ”€โ”€ nginx/
โ”‚   โ””โ”€โ”€ default.conf
โ”œโ”€โ”€ app/
โ”‚   โ”œโ”€โ”€ Dockerfile
โ”‚   โ””โ”€โ”€ index.js
โ”œโ”€โ”€ docker-compose.yml
โ””โ”€โ”€ .env

๐Ÿ“„ docker-compose.yml

version: "3.9"
services:
  app:
    build: ./app
    environment:
      - MONGO_URL=mongodb://mongo:27017/mydb
    depends_on:
      - mongo
    networks:
      - backend

  mongo:
    image: mongo
    volumes:
      - mongodata:/data/db
    networks:
      - backend

  nginx:
    image: nginx:latest
    ports:
      - "8080:80"
    volumes:
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
    depends_on:
      - app
    networks:
      - backend

volumes:
  mongodata:

networks:
  backend:

๐Ÿ” Service Dependencies & Health Checks

depends_on ensures order, but doesn't wait for readiness. To solve that:

Add a healthcheck to your app:

services:
  app:
    ...
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:3000/health"]
      interval: 30s
      retries: 3

๐Ÿ” Environment Variables & .env

Create a .env file in the root directory:

PORT=3000
MONGO_URL=mongodb://mongo:27017/mydb

Reference inside docker-compose.yml like this:

environment:
  - PORT=${PORT}
  - MONGO_URL=${MONGO_URL}

๐Ÿงฑ App Dockerfile

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

๐Ÿงฉ Managing the Stack

๐Ÿš€ Launch all containers:

docker-compose up

๐Ÿ”„ Rebuild after changes:

docker-compose up --build

๐Ÿ›‘ Stop everything:

docker-compose down

๐Ÿ“ˆ Scaling Services

You can run multiple containers of a service (useful for APIs):

docker-compose up --scale app=3

๐Ÿ’ก Docker Compose will automatically load-balance traffic between them โ€” if your reverse proxy (like Nginx) is configured to do so.

๐ŸŒ Sample NGINX Config

# nginx/default.conf
server {
  listen 80;
  location / {
    proxy_pass http://app:3000;
  }
}

This proxies incoming HTTP traffic to our Node.js backend container using its service name as a hostname. Docker's internal DNS handles it.

๐Ÿ“‹ Summary

This was your first hands-on dive into orchestrating real-world, production-like setups with Docker Compose.

In our next post, we'll dive into ๐Ÿ“ฆ (Post 4): Docker Registries: Docker Hub, Private Registry & Image Sharing!

โ€” Blog by Aelify (ML2AI.com)

๐Ÿ“š Documentation Index