name: messenger-lab3 x-app-env: &app-env APP_HOST: "0.0.0.0" APP_PORT: "8080" APP_ENV: ${APP_ENV:-lab} SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600} SESSION_STORE: ${SESSION_STORE:-postgres+cache} SESSION_CACHE_TTL_SECONDS: ${SESSION_CACHE_TTL_SECONDS:-60} DATABASE_URL_FILE: /run/secrets/database_url CACHE_URL_FILE: /run/secrets/cache_url IDEMPOTENCY_TTL_SECONDS: ${IDEMPOTENCY_TTL_SECONDS:-86400} services: postgres: image: postgres:17.6-alpine restart: unless-stopped environment: POSTGRES_DB: messenger POSTGRES_USER: messenger POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password secrets: [postgres_password] command: ["postgres", "-c", "fsync=on", "-c", "synchronous_commit=on", "-c", "full_page_writes=on", "-c", "max_connections=100"] volumes: - pgdata:/var/lib/postgresql/data healthcheck: test: ["CMD-SHELL", "pg_isready -U messenger -d messenger"] interval: 3s timeout: 3s retries: 20 stop_grace_period: 30s networks: [backend] valkey: image: valkey/valkey:8.1.3-alpine restart: unless-stopped secrets: [valkey_password] command: - sh - -c - >- exec valkey-server --requirepass "$$(cat /run/secrets/valkey_password)" --appendonly yes --appendfsync everysec --save "" --maxmemory 64mb --maxmemory-policy volatile-ttl volumes: - valkeydata:/data healthcheck: test: ["CMD-SHELL", "REDISCLI_AUTH=\"$$(cat /run/secrets/valkey_password)\" valkey-cli ping | grep -q PONG"] interval: 3s timeout: 3s retries: 20 networks: [backend] migrate: build: . command: ["python", "-m", "app.migrate"] environment: *app-env secrets: [database_url, cache_url] depends_on: postgres: {condition: service_healthy} restart: "no" networks: [backend] api: build: . restart: unless-stopped environment: *app-env secrets: [database_url, cache_url] depends_on: migrate: {condition: service_completed_successfully} postgres: {condition: service_healthy} valkey: {condition: service_started} read_only: true tmpfs: ["/tmp", "/dev/shm"] cap_drop: [ALL] security_opt: ["no-new-privileges:true"] healthcheck: test: ["CMD", "python", "-c", "import urllib.request as u; u.urlopen('http://127.0.0.1:8080/health/ready', timeout=2)"] interval: 5s timeout: 3s retries: 20 start_period: 5s stop_grace_period: 25s networks: [backend] deploy: replicas: 2 resources: limits: cpus: '0.50' memory: 512M nginx: image: nginx:1.27-alpine ports: - "127.0.0.1:8080:80" volumes: - ./nginx.conf:/etc/nginx/nginx.conf:ro depends_on: api: condition: service_healthy networks: [backend] networks: backend: {} volumes: pgdata: {} valkeydata: {} secrets: postgres_password: {file: ./secrets/postgres_password} valkey_password: {file: ./secrets/valkey_password} database_url: {file: ./secrets/database_url} cache_url: {file: ./secrets/cache_url}