services: postgres: image: postgres:16.4-bookworm environment: POSTGRES_DB: messenger POSTGRES_USER: messenger POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?set POSTGRES_PASSWORD in .env} volumes: - postgres_data:/var/lib/postgresql/data healthcheck: test: ["CMD-SHELL", "pg_isready -U messenger -d messenger"] interval: 2s timeout: 3s retries: 30 migrate: build: . environment: DATABASE_URL: postgresql://messenger:${POSTGRES_PASSWORD}@postgres:5432/messenger depends_on: postgres: condition: service_healthy command: ["python", "-c", "import app; app.wait_for_database()"] restart: "no" api1: build: . environment: APP_HOST: 0.0.0.0 APP_PORT: 8080 DATABASE_URL: postgresql://messenger:${POSTGRES_PASSWORD}@postgres:5432/messenger SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600} INSTANCE_ID: api1 depends_on: migrate: condition: service_completed_successfully healthcheck: test: ["CMD", "python", "-c", "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8080/health/ready', timeout=2)"] interval: 2s timeout: 3s retries: 30 restart: unless-stopped api2: build: . environment: APP_HOST: 0.0.0.0 APP_PORT: 8080 DATABASE_URL: postgresql://messenger:${POSTGRES_PASSWORD}@postgres:5432/messenger SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600} INSTANCE_ID: api2 depends_on: migrate: condition: service_completed_successfully healthcheck: test: ["CMD", "python", "-c", "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8080/health/ready', timeout=2)"] interval: 2s timeout: 3s retries: 30 restart: unless-stopped load_balancer: image: nginx:1.27.2-alpine ports: - "8080:80" volumes: - ./nginx/default.conf:/etc/nginx/conf.d/default.conf:ro depends_on: api1: condition: service_healthy api2: condition: service_healthy healthcheck: test: ["CMD", "wget", "-q", "-O", "-", "http://127.0.0.1/health/live"] interval: 2s timeout: 3s retries: 30 restart: unless-stopped volumes: postgres_data: