services: postgres: image: postgres:16.10-bookworm environment: POSTGRES_DB: ${POSTGRES_DB:-messenger} POSTGRES_USER: ${POSTGRES_USER:-lab} POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?Set POSTGRES_PASSWORD in .env} volumes: - postgres_data:/var/lib/postgresql/data healthcheck: test: ["CMD-SHELL", "pg_isready -U $$POSTGRES_USER -d $$POSTGRES_DB"] interval: 2s timeout: 2s retries: 30 mem_limit: 512m cpus: 1.0 restart: unless-stopped networks: [storage] redis: image: redis:7.4.5-alpine command: ["redis-server", "--appendonly", "yes", "--appendfsync", "always", "--save", "60", "1"] volumes: - redis_data:/data healthcheck: test: ["CMD", "redis-cli", "ping"] interval: 2s timeout: 2s retries: 30 mem_limit: 256m cpus: 0.5 restart: unless-stopped networks: [storage] migrate: build: . command: ["python", "-m", "app.migrate"] environment: DATABASE_URL: postgresql://${POSTGRES_USER:-lab}:${POSTGRES_PASSWORD:?Set POSTGRES_PASSWORD in .env}@postgres:5432/${POSTGRES_DB:-messenger} depends_on: postgres: condition: service_healthy networks: [storage] restart: "no" api1: build: . environment: &api_env APP_PORT: "8080" API_INSTANCE_ID: api1 DATABASE_URL: postgresql://${POSTGRES_USER:-lab}:${POSTGRES_PASSWORD:?Set POSTGRES_PASSWORD in .env}@postgres:5432/${POSTGRES_DB:-messenger} REDIS_URL: redis://redis:6379/0 SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600} IDEMPOTENCY_TTL_SECONDS: ${IDEMPOTENCY_TTL_SECONDS:-86400} DB_POOL_MAX: ${DB_POOL_MAX:-8} depends_on: &api_depends postgres: condition: service_healthy redis: condition: service_healthy migrate: condition: service_completed_successfully expose: ["8080"] healthcheck: &api_health 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: 15 start_period: 10s mem_limit: 384m cpus: 0.75 stop_grace_period: 12s restart: unless-stopped networks: [frontend, storage] api2: build: . environment: <<: *api_env API_INSTANCE_ID: api2 depends_on: *api_depends expose: ["8080"] healthcheck: *api_health mem_limit: 384m cpus: 0.75 stop_grace_period: 12s restart: unless-stopped networks: [frontend, storage] lb: image: haproxy:3.1.7-alpine volumes: - ./infra/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro ports: - "127.0.0.1:8080:8080" depends_on: api1: condition: service_healthy api2: condition: service_healthy healthcheck: test: ["CMD", "haproxy", "-c", "-f", "/usr/local/etc/haproxy/haproxy.cfg"] interval: 10s timeout: 3s retries: 3 mem_limit: 128m cpus: 0.5 restart: unless-stopped networks: [frontend] networks: frontend: storage: internal: true volumes: postgres_data: redis_data: