# Client -> lb (HAProxy) -> api x API_REPLICAS -> postgres + minio + rabbitmq. # HTTP upload still receives the whole body. POST returns 202 after staging and the job row # commit. The outbox relay confirms the job to RabbitMQ; a crash between those two is the # same row, republished later. The worker writes the original, a 256 JPEG thumbnail, a # 128 JPEG crop and a WebP fit inside 1280. Worker memory limit below is the decode cap. # Only the LB publishes a port. Networks: edge = lb<->api, data = api/worker/stores (no LB, no internet). # Passwords come from .env (not committed) as Compose secret files, never as container env vars. name: messenger-lab5 x-db-client: &db-client APP_ENV: ${APP_ENV:-lab} LOG_LEVEL: ${LOG_LEVEL:-info} DB_HOST: postgres DB_USER: ${POSTGRES_USER:?set in .env} DB_NAME: ${POSTGRES_DB:?set in .env} DB_PASSWORD_FILE: /run/secrets/postgres_password x-app-build: &app-build context: . dockerfile: Dockerfile args: APP_VERSION: ${APP_VERSION:-dev} services: postgres: image: postgres:16.10-alpine@sha256:029660641a0cfc575b14f336ba448fb8a75fd595d42e1fa316b9fb4378742297 environment: POSTGRES_USER: ${POSTGRES_USER} POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password POSTGRES_DB: ${POSTGRES_DB} secrets: - postgres_password volumes: - pgdata:/var/lib/postgresql/data networks: - data healthcheck: test: ["CMD-SHELL", 'pg_isready -U "$$POSTGRES_USER" -d "$$POSTGRES_DB"'] interval: 2s timeout: 5s retries: 30 deploy: resources: limits: cpus: "${POSTGRES_CPUS:-1.0}" memory: "${POSTGRES_MEMORY:-512M}" reservations: cpus: "0.25" memory: 256M restart: unless-stopped # One-shot schema step. Replicas never migrate on startup, so there is no race between them; # an advisory lock additionally serializes accidental parallel runs. migrate: image: messenger-lab5-app:${APP_VERSION:-dev} build: *app-build command: ["python", "-m", "app.migrate"] environment: <<: *db-client secrets: - source: postgres_password uid: "10001" gid: "10001" mode: 0400 networks: - data depends_on: postgres: condition: service_healthy deploy: resources: limits: cpus: "0.5" memory: 128M restart: "no" # MinIO Community RELEASE.2025-09-07T16-13-09Z. The digest is the official # multi-arch manifest list (linux/amd64 inside it is sha256:a1a8bd4a…). # docker.io/minio/minio and quay.io/minio/minio no longer allow an anonymous # pull; this GHCR mirror still serves that same manifest list. # No host ports. MINIO_BROWSER=off: the admin console is not started. # S3 API is http://minio:9000, also http://s3:9000 (network alias). minio: image: ghcr.io/tsouza/cerberus-mirror/minio/minio:RELEASE.2025-09-07T16-13-09Z@sha256:14cea493d9a34af32f524e538b8346cf79f3321eff8e708c1e2960462bd8936e command: ["server", "/data"] environment: MINIO_ROOT_USER_FILE: /run/secrets/minio_root_user MINIO_ROOT_PASSWORD_FILE: /run/secrets/minio_root_password MINIO_BROWSER: "off" MINIO_UPDATE: "off" secrets: - minio_root_user - minio_root_password volumes: - miniodata:/data networks: data: aliases: - s3 healthcheck: test: ["CMD", "curl", "-fsS", "http://127.0.0.1:9000/minio/health/ready"] interval: 2s timeout: 5s retries: 30 start_period: 10s deploy: resources: limits: cpus: "${MINIO_CPUS:-1.0}" memory: "${MINIO_MEMORY:-512M}" reservations: cpus: "0.25" memory: 256M restart: unless-stopped # Creates the private bucket and the bucket-scoped API user. Re-running is a no-op. minio-init: image: ghcr.io/tsouza/cerberus-mirror/minio/minio:RELEASE.2025-09-07T16-13-09Z@sha256:14cea493d9a34af32f524e538b8346cf79f3321eff8e708c1e2960462bd8936e entrypoint: ["/bin/sh", "/bootstrap/minio-bootstrap.sh"] environment: S3_BUCKET: ${S3_BUCKET:?set in .env} S3_REGION: ${S3_REGION:-us-east-1} MINIO_ENDPOINT: http://minio:9000 secrets: - minio_root_user - minio_root_password - s3_access_key_id - s3_secret_access_key volumes: - ./infra/minio/bootstrap.sh:/bootstrap/minio-bootstrap.sh:ro networks: - data depends_on: minio: condition: service_healthy deploy: resources: limits: cpus: "0.5" memory: 256M restart: "no" # Stable hostname: the data directory is rabbit@rabbitmq, so a new container keeps old jobs. rabbitmq: image: rabbitmq:4.1-alpine@sha256:cd4009416f83d7e5895020cd71b0963f3b8a4dfc3de74bbb6fb3bfe5f81f01e0 hostname: rabbitmq entrypoint: ["/bin/sh", "/bootstrap/rabbitmq-entrypoint.sh"] secrets: - rabbitmq_user - rabbitmq_password volumes: - ./infra/rabbitmq/entrypoint.sh:/bootstrap/rabbitmq-entrypoint.sh:ro - rabbitmqdata:/var/lib/rabbitmq networks: - data healthcheck: test: ["CMD", "rabbitmq-diagnostics", "-q", "ping"] interval: 5s timeout: 10s retries: 12 start_period: 20s deploy: resources: limits: cpus: "1.0" memory: 512M reservations: cpus: "0.25" memory: 256M stop_grace_period: 30s restart: unless-stopped api: image: messenger-lab5-app:${APP_VERSION:-dev} build: *app-build deploy: replicas: ${API_REPLICAS:-2} resources: limits: cpus: "${API_CPUS:-1.0}" memory: "${API_MEMORY:-256M}" reservations: cpus: "0.25" memory: 128M environment: <<: *db-client APP_HOST: 0.0.0.0 APP_PORT: "8080" SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600} IDEMPOTENCY_TTL_SECONDS: ${IDEMPOTENCY_TTL_SECONDS:-86400} DRAIN_SECONDS: ${DRAIN_SECONDS:-4} SHUTDOWN_TIMEOUT_SECONDS: ${SHUTDOWN_TIMEOUT_SECONDS:-3} DB_POOL_MIN: ${DB_POOL_MIN:-1} DB_POOL_MAX: ${DB_POOL_MAX:-10} S3_ENDPOINT: ${S3_ENDPOINT:?set in .env} S3_REGION: ${S3_REGION:-us-east-1} S3_BUCKET: ${S3_BUCKET:?set in .env} S3_ACCESS_KEY_ID_FILE: /run/secrets/s3_access_key_id S3_SECRET_ACCESS_KEY_FILE: /run/secrets/s3_secret_access_key BROKER_URL_FILE: /run/secrets/broker_url JOB_MAX_ATTEMPTS: ${JOB_MAX_ATTEMPTS:-5} JOB_TIMEOUT_SECONDS: ${JOB_TIMEOUT_SECONDS:-60} WORKER_CONCURRENCY: ${WORKER_CONCURRENCY:-1} # queued + processing. Past this, upload is 429 and the bytes are not stored. MAX_INFLIGHT_JOBS: ${MAX_INFLIGHT_JOBS:-20} secrets: - source: postgres_password uid: "10001" gid: "10001" mode: 0400 - source: s3_access_key_id uid: "10001" gid: "10001" mode: 0400 - source: s3_secret_access_key uid: "10001" gid: "10001" mode: 0400 - source: broker_url uid: "10001" gid: "10001" mode: 0400 networks: - edge - data expose: - "8080" depends_on: postgres: condition: service_healthy migrate: condition: service_completed_successfully minio-init: condition: service_completed_successfully rabbitmq: condition: service_healthy # Docker health gates startup order and rolling update; the LB runs its own checks (infra/haproxy/haproxy.cfg). healthcheck: test: - CMD - python - -c - import urllib.request; urllib.request.urlopen('http://127.0.0.1:8080/health/ready', timeout=2) interval: 3s timeout: 3s retries: 3 start_period: 20s # SIGTERM -> readiness 503 for DRAIN_SECONDS (LB drops the replica in <= 3 s) -> uvicorn finishes # in-flight requests within SHUTDOWN_TIMEOUT_SECONDS. 4 + 3 < 10, so SIGKILL is not reached. stop_signal: SIGTERM stop_grace_period: 10s restart: unless-stopped worker: image: messenger-lab5-app:${APP_VERSION:-dev} command: ["python", "-m", "app.worker"] environment: <<: *db-client S3_ENDPOINT: ${S3_ENDPOINT:?set in .env} S3_REGION: ${S3_REGION:-us-east-1} S3_BUCKET: ${S3_BUCKET:?set in .env} S3_ACCESS_KEY_ID_FILE: /run/secrets/s3_access_key_id S3_SECRET_ACCESS_KEY_FILE: /run/secrets/s3_secret_access_key BROKER_URL_FILE: /run/secrets/broker_url JOB_MAX_ATTEMPTS: ${JOB_MAX_ATTEMPTS:-5} JOB_TIMEOUT_SECONDS: ${JOB_TIMEOUT_SECONDS:-60} JOB_RETRY_BASE_SECONDS: ${JOB_RETRY_BASE_SECONDS:-0.5} JOB_RETRY_CAP_SECONDS: ${JOB_RETRY_CAP_SECONDS:-30} JOB_LEASE_SECONDS: ${JOB_LEASE_SECONDS:-90} STAGING_RETENTION_SECONDS: ${STAGING_RETENTION_SECONDS:-86400} WORKER_CONCURRENCY: ${WORKER_CONCURRENCY:-1} DB_POOL_MIN: "1" # Two in-flight jobs hold a connection each, plus a lease heartbeat and the outbox relay. DB_POOL_MAX: "8" secrets: - source: postgres_password uid: "10001" gid: "10001" mode: 0400 - source: s3_access_key_id uid: "10001" gid: "10001" mode: 0400 - source: s3_secret_access_key uid: "10001" gid: "10001" mode: 0400 - source: broker_url uid: "10001" gid: "10001" mode: 0400 networks: - data depends_on: postgres: condition: service_healthy migrate: condition: service_completed_successfully minio-init: condition: service_completed_successfully rabbitmq: condition: service_healthy healthcheck: test: - CMD - python - -c - import os,sys,time; p='/tmp/worker-alive'; sys.exit(0 if os.path.exists(p) and time.time()-os.path.getmtime(p)<20 else 1) interval: 5s timeout: 3s retries: 6 start_period: 25s stop_signal: SIGTERM stop_grace_period: 30s deploy: resources: limits: cpus: "1.0" memory: 512M reservations: cpus: "0.25" memory: 256M restart: unless-stopped lb: image: haproxy:3.2.26-alpine@sha256:96ec3c75a9264ba94e733eeacb44051ed6e7b79bb77a73c2c36f236b6423f1f0 # Loopback only. Both families: on Windows "localhost" tries ::1 first and waits ~2 s if nothing listens. ports: - "127.0.0.1:${LB_PORT:-8080}:8080" - "[::1]:${LB_PORT:-8080}:8080" volumes: - ./infra/haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro networks: - edge depends_on: api: condition: service_healthy healthcheck: test: ["CMD", "wget", "-qO-", "http://127.0.0.1:8080/lb/health"] interval: 5s timeout: 3s retries: 3 deploy: resources: limits: cpus: "0.5" memory: 128M reservations: cpus: "0.1" memory: 32M restart: unless-stopped networks: edge: {} data: internal: true secrets: postgres_password: environment: POSTGRES_PASSWORD minio_root_user: environment: MINIO_ROOT_USER minio_root_password: environment: MINIO_ROOT_PASSWORD s3_access_key_id: environment: S3_ACCESS_KEY_ID s3_secret_access_key: environment: S3_SECRET_ACCESS_KEY rabbitmq_user: environment: RABBITMQ_USER rabbitmq_password: environment: RABBITMQ_PASSWORD broker_url: environment: BROKER_URL volumes: pgdata: miniodata: rabbitmqdata: