# Kivo from the published Docker images (invicly/kivo and invicly/kivo-migrate):
# PostgreSQL, a one-off migration job, the app, and Caddy for HTTPS.
#
#   curl -fsSL https://kivo-control.invicly.com/install/files/docker-compose.yml -o docker-compose.yml
#   curl -fsSL https://kivo-control.invicly.com/install/files/kivo.env -o .env && chmod 600 .env
#   # fill in .env, then:
#   docker compose up -d
#
# COMPOSE_PROFILES in .env picks the optional parts: "db" runs PostgreSQL here (leave it
# out when DATABASE_URL points at a managed database), "https" runs Caddy with a
# Let's Encrypt certificate for KIVO_DOMAIN (leave it out behind your own proxy).
# The images are built for linux/amd64; Docker Desktop on Apple silicon runs them emulated.
name: kivo

x-database-url: &database-url
  DATABASE_URL: ${DATABASE_URL:-postgresql://${POSTGRES_USER:-kivo}:${POSTGRES_PASSWORD:-}@postgres:5432/${POSTGRES_DB:-kivo}?schema=public}

services:
  postgres:
    profiles: ["db"]
    image: postgres:16-alpine
    restart: unless-stopped
    environment:
      POSTGRES_USER: ${POSTGRES_USER:-kivo}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-}
      POSTGRES_DB: ${POSTGRES_DB:-kivo}
    volumes:
      - postgres-data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-kivo} -d ${POSTGRES_DB:-kivo}"]
      interval: 5s
      timeout: 5s
      retries: 20

  # Applies pending migrations, then exits. The app starts only if it succeeded.
  migrate:
    image: ${KIVO_REGISTRY-invicly/}kivo-migrate:${KIVO_VERSION:-1.0.0}
    platform: linux/amd64
    depends_on:
      postgres:
        condition: service_healthy
        required: false
    environment:
      <<: *database-url
    restart: "no"

  app:
    image: ${KIVO_REGISTRY-invicly/}kivo:${KIVO_VERSION:-1.0.0}
    platform: linux/amd64
    restart: unless-stopped
    depends_on:
      migrate:
        condition: service_completed_successfully
    # Every setting comes from the one .env file.
    env_file: .env
    environment:
      <<: *database-url
      NODE_ENV: production
      AUTH_TRUST_HOST: "true"
      EMAIL_OUTBOX_DIR: /tmp/outbox
    ports:
      # Only on this machine; people reach Kivo through Caddy or your own proxy.
      # Behind a cloud load balancer, set APP_BIND=0.0.0.0 and allow port 3000 only from it.
      - "${APP_BIND:-127.0.0.1}:${APP_PORT:-3000}:3000"
    read_only: true
    tmpfs:
      - /tmp
      - /app/.next/cache
    security_opt:
      - no-new-privileges:true
    cap_drop:
      - ALL
    healthcheck:
      test: ["CMD-SHELL", "wget -qO- http://127.0.0.1:3000/api/health/ready >/dev/null || exit 1"]
      interval: 15s
      timeout: 5s
      start_period: 30s
      retries: 3

  # HTTPS with an automatic Let's Encrypt certificate (ports 80 and 443 must be open).
  caddy:
    profiles: ["https"]
    image: caddy:2-alpine
    restart: unless-stopped
    depends_on:
      - app
    command: caddy reverse-proxy --from ${KIVO_DOMAIN:-} --to app:3000
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - caddy-data:/data
      - caddy-config:/config

volumes:
  postgres-data:
  caddy-data:
  caddy-config:
