# Client -> lb (HAProxy) -> api x N -> postgres (users, chats, messages, sessions, idempotency keys). # Start: scripts/init-secrets.sh && docker compose --env-file .env up -d --build (see REPORT.md) name: messenger-lab3 x-api: &api build: . image: messenger-api:lab3 environment: DATABASE_URL_FILE: /run/secrets/database_url SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600} DB_POOL_MAX: ${DB_POOL_MAX:-8} SHUTDOWN_DRAIN_MS: ${SHUTDOWN_DRAIN_MS:-2500} secrets: [database_url] networks: [backend] services: postgres: image: postgres:18.6-alpine command: ["postgres", "-c", "max_connections=200", "-c", "shared_buffers=256MB"] environment: POSTGRES_USER: messenger POSTGRES_DB: messenger POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password secrets: [postgres_password] volumes: [pgdata:/var/lib/postgresql] healthcheck: test: ["CMD-SHELL", "pg_isready -U messenger -d messenger"] interval: 2s timeout: 2s retries: 15 deploy: resources: limits: { cpus: "${DB_CPUS:-2}", memory: 1g } stop_grace_period: 30s restart: unless-stopped networks: [backend] # One-shot: schema migrations run once before any replica starts (advisory lock guards manual reruns). migrate: <<: *api command: ["node", "src/main.ts", "migrate"] restart: "no" depends_on: postgres: { condition: service_healthy } api: <<: *api depends_on: migrate: { condition: service_completed_successfully } healthcheck: test: ["CMD", "wget", "-qO", "/dev/null", "http://127.0.0.1:8080/health/ready"] interval: 2s timeout: 2s retries: 3 start_period: 10s deploy: replicas: ${API_REPLICAS:-2} resources: limits: { cpus: "${API_CPUS:-1}", memory: "${API_MEMORY:-256m}" } stop_grace_period: 15s restart: unless-stopped # a crashed replica comes back; `docker stop` keeps it stopped lb: image: haproxy:3.2.26-alpine volumes: ["./infra/haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro"] ports: - "127.0.0.1:${LB_PORT:-8080}:8080" - "127.0.0.1:${LB_STATS_PORT:-8404}:8404" # HAProxy stats/metrics for the operator, not the API depends_on: api: { condition: service_healthy } deploy: resources: limits: { cpus: "${LB_CPUS:-1}", memory: 128m } restart: unless-stopped networks: [frontend, backend] networks: frontend: {} backend: { internal: true } # api and postgres are unreachable from the host and have no egress volumes: pgdata: {} secrets: database_url: { file: ./secrets/database_url } postgres_password: { file: ./secrets/postgres_password }