name: ftb-project-management

services:
  postgres:
    image: postgres:16-alpine
    restart: unless-stopped
    environment:
      POSTGRES_USER: ${POSTGRES_USER:-postgres}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?Set POSTGRES_PASSWORD in .env.production}
      POSTGRES_DB: ${POSTGRES_DB:-ftb_pm}
    volumes:
      - postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: ['CMD-SHELL', 'pg_isready -U "$${POSTGRES_USER}" -d "$${POSTGRES_DB}"']
      interval: 10s
      timeout: 5s
      retries: 10

  redis:
    image: redis:7-alpine
    restart: unless-stopped
    command: ['redis-server', '--appendonly', 'yes']
    volumes:
      - redis_data:/data
    healthcheck:
      test: ['CMD', 'redis-cli', 'ping']
      interval: 10s
      timeout: 5s
      retries: 10

  server:
    image: ${SERVER_IMAGE:?Set SERVER_IMAGE in .env.production}
    build:
      context: .
      dockerfile: Dockerfile.server
      args:
        APP_VERSION: ${APP_VERSION:-unknown}
        APP_BUILD_TIME: ${APP_BUILD_TIME:-}
        APP_IMAGE_TAG: ${SERVER_IMAGE:-}
    restart: unless-stopped
    environment:
      NODE_ENV: production
      APP_VERSION: ${APP_VERSION:-unknown}
      APP_BUILD_TIME: ${APP_BUILD_TIME:-}
      APP_IMAGE_TAG: ${SERVER_IMAGE:-}
      DATABASE_URL: ${DATABASE_URL:-postgresql://${POSTGRES_USER:-postgres}:${POSTGRES_PASSWORD}@postgres:5432/${POSTGRES_DB:-ftb_pm}}
      REDIS_URL: ${REDIS_URL:-redis://redis:6379}
      ANTHROPIC_API_KEY: ${ANTHROPIC_API_KEY:-}
      SMTP_HOST: ${SMTP_HOST:-}
      SMTP_PORT: ${SMTP_PORT:-}
      SMTP_USER: ${SMTP_USER:-}
      SMTP_PASS: ${SMTP_PASS:-}
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    volumes:
      - server_data:/app/apps/server/data
    expose:
      - '3001'
    healthcheck:
      test:
        [
          'CMD-SHELL',
          'node -e "fetch(''http://127.0.0.1:3001/api/v1/health/version'').then(r=>process.exit(r.ok?0:1)).catch(()=>process.exit(1))"',
        ]
      interval: 15s
      timeout: 5s
      retries: 20
      start_period: 20s

  web:
    image: ${WEB_IMAGE:?Set WEB_IMAGE in .env.production}
    build:
      context: .
      dockerfile: Dockerfile.web
      args:
        NEXT_PUBLIC_API_URL: ${NEXT_PUBLIC_API_URL:-/api/v1}
        NEXT_API_PROXY_TARGET: ${NEXT_API_PROXY_TARGET:-http://server:3001}
        APP_VERSION: ${APP_VERSION:-unknown}
        APP_BUILD_TIME: ${APP_BUILD_TIME:-}
        APP_IMAGE_TAG: ${WEB_IMAGE:-}
    restart: unless-stopped
    environment:
      NODE_ENV: production
      NEXT_PUBLIC_API_URL: ${NEXT_PUBLIC_API_URL:-/api/v1}
      NEXT_API_PROXY_TARGET: ${NEXT_API_PROXY_TARGET:-http://server:3001}
      NEXT_PUBLIC_APP_VERSION: ${APP_VERSION:-unknown}
      NEXT_PUBLIC_APP_BUILD_TIME: ${APP_BUILD_TIME:-}
      NEXT_PUBLIC_APP_IMAGE_TAG: ${WEB_IMAGE:-}
      NEXTAUTH_URL: ${NEXTAUTH_URL:-}
      NEXTAUTH_SECRET: ${NEXTAUTH_SECRET:-}
    depends_on:
      server:
        condition: service_healthy
    expose:
      - '3000'
    healthcheck:
      test:
        [
          'CMD-SHELL',
          'node -e "fetch(''http://127.0.0.1:3000'').then(r=>process.exit(r.ok?0:1)).catch(()=>process.exit(1))"',
        ]
      interval: 15s
      timeout: 5s
      retries: 20
      start_period: 20s

  nginx:
    image: nginx:1.27-alpine
    restart: unless-stopped
    ports:
      - '${HTTP_PORT:-80}:80'
    environment:
      SERVER_NAME: ${SERVER_NAME:-_}
      CLIENT_MAX_BODY_SIZE: ${CLIENT_MAX_BODY_SIZE:-20m}
    volumes:
      - ./deploy/nginx/default.conf.template:/etc/nginx/templates/default.conf.template:ro
    depends_on:
      web:
        condition: service_healthy
      server:
        condition: service_healthy

  prometheus:
    image: prom/prometheus:v2.53.1
    profiles: ['monitoring']
    restart: unless-stopped
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
      - '--storage.tsdb.path=/prometheus'
      - '--storage.tsdb.retention.time=${PROMETHEUS_RETENTION:-15d}'
      - '--web.enable-lifecycle'
    ports:
      - '${PROMETHEUS_PORT:-9090}:9090'
    volumes:
      - ./deploy/monitoring/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml:ro
      - ./deploy/monitoring/prometheus/alert-rules.yml:/etc/prometheus/alert-rules.yml:ro
      - prometheus_data:/prometheus
    depends_on:
      - postgres-exporter
      - node-exporter
      - cadvisor
      - promtail
      - blackbox-exporter

  grafana:
    image: grafana/grafana:11.1.0
    profiles: ['monitoring']
    restart: unless-stopped
    ports:
      - '${GRAFANA_PORT:-3002}:3000'
    environment:
      GF_SECURITY_ADMIN_USER: ${GRAFANA_ADMIN_USER:-admin}
      GF_SECURITY_ADMIN_PASSWORD: ${GRAFANA_ADMIN_PASSWORD:-change-me-monitoring-password}
      GF_USERS_ALLOW_SIGN_UP: 'false'
    volumes:
      - grafana_data:/var/lib/grafana
      - ./deploy/monitoring/grafana/provisioning:/etc/grafana/provisioning:ro
      - ./deploy/monitoring/grafana/dashboards:/var/lib/grafana/dashboards:ro
    depends_on:
      - prometheus
      - loki

  loki:
    image: grafana/loki:2.9.8
    profiles: ['monitoring']
    restart: unless-stopped
    command: ['-config.file=/etc/loki/config.yml']
    volumes:
      - ./deploy/monitoring/loki/config.yml:/etc/loki/config.yml:ro
      - loki_data:/loki

  promtail:
    image: grafana/promtail:2.9.8
    profiles: ['monitoring']
    restart: unless-stopped
    command: ['-config.file=/etc/promtail/config.yml']
    volumes:
      - ./deploy/monitoring/promtail/config.yml:/etc/promtail/config.yml:ro
      - /var/lib/docker/containers:/var/lib/docker/containers:ro
      - /var/run/docker.sock:/var/run/docker.sock:ro
    depends_on:
      - loki

  postgres-exporter:
    image: quay.io/prometheuscommunity/postgres-exporter:v0.15.0
    profiles: ['monitoring']
    restart: unless-stopped
    command:
      - '--extend.query-path=/etc/postgres-exporter/postgres-queries.yml'
    environment:
      DATA_SOURCE_NAME: postgresql://${POSTGRES_USER:-postgres}:${POSTGRES_PASSWORD}@postgres:5432/${POSTGRES_DB:-ftb_pm}?sslmode=disable
    volumes:
      - ./deploy/monitoring/postgres/postgres-queries.yml:/etc/postgres-exporter/postgres-queries.yml:ro
    depends_on:
      postgres:
        condition: service_healthy

  node-exporter:
    image: prom/node-exporter:v1.8.2
    profiles: ['monitoring']
    restart: unless-stopped
    command:
      - '--path.rootfs=/host'
    volumes:
      - /:/host:ro,rslave

  cadvisor:
    image: gcr.io/cadvisor/cadvisor:v0.49.1
    profiles: ['monitoring']
    restart: unless-stopped
    privileged: true
    devices:
      - /dev/kmsg:/dev/kmsg
    volumes:
      - /:/rootfs:ro
      - /var/run:/var/run:ro
      - /sys:/sys:ro
      - /var/lib/docker/:/var/lib/docker:ro
      - /dev/disk/:/dev/disk:ro

  blackbox-exporter:
    image: prom/blackbox-exporter:v0.25.0
    profiles: ['monitoring']
    restart: unless-stopped
    command:
      - '--config.file=/etc/blackbox/config.yml'
    volumes:
      - ./deploy/monitoring/blackbox/config.yml:/etc/blackbox/config.yml:ro

volumes:
  postgres_data:
  redis_data:
  server_data:
  prometheus_data:
  grafana_data:
  loki_data:
