Skillversitas

Docker Compose Advanced

Advanced Docker Compose features for production applications.

Profiles

services:
  web:
    image: nginx:alpine

  debug:
    image: busybox
    profiles:
      - debug

  test:
    image: myapp:test
    profiles:
      - test

# Run: docker-compose --profile debug up

Resource Limits

services:
  api:
    build: ./backend
    deploy:
      resources:
        limits:
          cpus: '1.0'
          memory: 512M
        reservations:
          cpus: '0.5'
          memory: 256M

Health Checks

services:
  api:
    build: ./backend
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:3000/health"]
      interval: 30s
      timeout: 3s
      retries: 3
      start_period: 10s
    depends_on:
      database:
        condition: service_healthy

Use Case: Advanced Compose

version: '3.8'

services:
  frontend:
    build:
      context: ./frontend
      dockerfile: Dockerfile
      args:
        NODE_ENV: production
    ports:
      - "80:80"
    networks:
      - frontend-net

  api:
    build: ./backend
    environment:
      - NODE_ENV=production
      - DB_HOST=database
    volumes:
      - ./backend/src:/app/src
    deploy:
      resources:
        limits:
          cpus: '1.0'
          memory: 512M
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:3000/health"]
      interval: 30s
      timeout: 3s
      retries: 3
    networks:
      - frontend-net
      - backend-net
    depends_on:
      database:
        condition: service_healthy

  database:
    image: postgres:15
    environment:
      POSTGRES_DB: myapp
      POSTGRES_USER: admin
      POSTGRES_PASSWORD_FILE: /run/secrets/db_password
    volumes:
      - db-data:/var/lib/postgresql/data
    secrets:
      - db_password
    networks:
      - backend-net
    deploy:
      resources:
        limits:
          cpus: '2.0'
          memory: 2G

  nginx:
    image: nginx:alpine
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
    ports:
      - "443:443"
    depends_on:
      - api
    networks:
      - frontend-net

networks:
  frontend-net:
  backend-net:

volumes:
  db-data:

secrets:
  db_password:
    file: ./secrets/db_password.txt

Everyday Use Case

Case: A developer needs a production configuration.

# Run with profiles
docker-compose --profile monitoring up -d

# View the status
docker-compose ps

# View resource usage
docker stats --no-stream

Environment Override

# docker-compose.override.yml
version: '3.8'

services:
  api:
    environment:
      - NODE_ENV=development
    volumes:
      - ./backend/src:/app/src

Best Practices

  1. Use profiles: For additional features
  2. Resource limits: Set them for every service
  3. Health checks: For healthy dependencies
  4. Secrets management: Use Docker secrets
  5. Override files: For different environments

Conclusion

Docker Compose Advanced enables more complex configuration for production. Use profiles, resource limits, and health checks for a robust application.