Docker Microservices

This chapter covers how to build and deploy microservices architecture applications using Docker.

Microservices Overview

Microservices architecture splits applications into multiple independent services, each:

  • Independently developed and deployed
  • Owning its own data store
  • Communicating via APIs
  • Potentially using different technology stacks
┌─────────────────────────────────────────────────┐
│                   API Gateway                    │
│                  (Nginx/Traefik)                 │
└──────┬──────────────┬──────────────┬────────────┘
       │              │              │
┌──────▼─────┐ ┌──────▼─────┐ ┌─────▼──────┐
│ User Svc   │ │ Order Svc  │ │Product Svc │
│ (Node.js)  │ │ (Python)   │ │ (Go)       │
└──────┬─────┘ └──────┬─────┘ └─────┬──────┘
       │              │              │
┌──────▼─────┐ ┌──────▼─────┐ ┌─────▼──────┐
│ PostgreSQL │ │   MySQL    │ │  MongoDB   │
└────────────┘ └────────────┘ └────────────┘

Docker Compose Implementation

docker-compose.yml

services:
  gateway:
    image: nginx:alpine
    ports:
      - "80:80"
    volumes:
      - ./gateway/nginx.conf:/etc/nginx/conf.d/default.conf
    depends_on:
      - user-service
      - order-service

  user-service:
    build: ./user-service
    environment:
      - DB_HOST=user-db
    depends_on:
      - user-db

  user-db:
    image: postgres:16
    environment:
      POSTGRES_DB: users
      POSTGRES_PASSWORD: secret
    volumes:
      - user-db-data:/var/lib/postgresql/data

  order-service:
    build: ./order-service
    environment:
      - DB_HOST=order-db
    depends_on:
      - order-db

  order-db:
    image: mysql:8.0
    environment:
      MYSQL_DATABASE: orders
      MYSQL_ROOT_PASSWORD: secret
    volumes:
      - order-db-data:/var/lib/mysql

  redis:
    image: redis:7-alpine
    volumes:
      - redis-data:/data

volumes:
  user-db-data:
  order-db-data:
  redis-data:

API Gateway Configuration

upstream user-service {
    server user-service:3000;
}
upstream order-service {
    server order-service:8000;
}

server {
    listen 80;
    location /api/users { proxy_pass http://user-service; }
    location /api/orders { proxy_pass http://order-service; }
}

Service Scaling

docker compose up -d --scale order-service=3

Health Checks

services:
  user-service:
    build: ./user-service
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:3000/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s

Best Practices

  1. One service per container: Follow single responsibility principle
  2. Use custom networks: Isolate different service groups
  3. Environment variable configuration: Don't hardcode config
  4. Health checks: Ensure service availability
  5. Data persistence: Use named volumes
  6. Centralized logging: Easier troubleshooting
  7. Graceful shutdown: Handle SIGTERM signals

Chapter Summary

Docker and Docker Compose are ideal tools for building microservices. Containerizing each service enables independent deployment, flexible scaling, and technology diversity.

Further Reading