name: minmon

services:
  postgres:
    image: postgres:17-alpine
    environment:
      POSTGRES_DB: ${POSTGRES_DB:?POSTGRES_DB must be set in root .env}
      POSTGRES_USER: ${POSTGRES_USER:?POSTGRES_USER must be set in root .env}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?POSTGRES_PASSWORD must be set in root .env}
    ports:
      - "${POSTGRES_PORT:?POSTGRES_PORT must be set in root .env}:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U $${POSTGRES_USER} -d $${POSTGRES_DB}"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 10s
    restart: unless-stopped

  db-init:
    build:
      context: .
      dockerfile: apps/server/Dockerfile
      target: tools
    environment:
      <<: *server-environment
    command: ["sh", "-c", "bun run db:push && bun run --filter @minmon/db db:seed:if-empty"]
    depends_on:
      postgres:
        condition: service_healthy
    restart: "no"

  server:
    build:
      context: .
      dockerfile: apps/server/Dockerfile
    environment:
      <<: *server-environment
    ports:
      - "${SERVER_PORT:?SERVER_PORT must be set in root .env}:3000"
    depends_on:
      postgres:
        condition: service_healthy
      db-init:
        condition: service_completed_successfully
    restart: unless-stopped

  web:
    build:
      context: .
      dockerfile: apps/web/Dockerfile
      args:
        VITE_SERVER_URL: ${VITE_SERVER_URL:?VITE_SERVER_URL must be set in root .env}
    ports:
      - "${WEB_PORT:?WEB_PORT must be set in root .env}:80"
    depends_on:
      server:
        condition: service_started
    restart: unless-stopped

volumes:
  postgres_data:
