Files
mtusi2026lab3/compose.yaml
T

97 lines
2.9 KiB
YAML

# Client -> lb (nginx) -> api x API_REPLICAS -> postgres.
# Secrets come from .env, which is not committed. Only the LB publishes a port.
name: messenger-lab3
x-app-env: &app-env
APP_ENV: ${APP_ENV:-lab}
LOG_LEVEL: ${LOG_LEVEL:-info}
DATABASE_URL: postgresql://${POSTGRES_USER:?set in .env}:${POSTGRES_PASSWORD:?set in .env}@postgres:5432/${POSTGRES_DB:?set in .env}
services:
postgres:
image: postgres:16.10-alpine@sha256:029660641a0cfc575b14f336ba448fb8a75fd595d42e1fa316b9fb4378742297
environment:
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: ${POSTGRES_DB}
volumes:
- pgdata:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U \"$$POSTGRES_USER\" -d \"$$POSTGRES_DB\""]
interval: 2s
timeout: 5s
retries: 30
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-lab3-app:local
build:
context: .
dockerfile: Dockerfile
command: ["python", "-m", "app.migrate"]
environment:
<<: *app-env
depends_on:
postgres:
condition: service_healthy
restart: "no"
api:
image: messenger-lab3-app:local
build:
context: .
dockerfile: Dockerfile
deploy:
replicas: ${API_REPLICAS:-2}
environment:
<<: *app-env
APP_HOST: 0.0.0.0
APP_PORT: "8080"
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
IDEMPOTENCY_TTL_SECONDS: ${IDEMPOTENCY_TTL_SECONDS:-86400}
SHUTDOWN_TIMEOUT_SECONDS: ${SHUTDOWN_TIMEOUT_SECONDS:-5}
expose:
- "8080"
depends_on:
postgres:
condition: service_healthy
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: 3s
timeout: 3s
retries: 3
start_period: 20s
# SIGTERM -> uvicorn stops accepting, finishes in-flight requests within SHUTDOWN_TIMEOUT_SECONDS.
stop_signal: SIGTERM
stop_grace_period: 8s
restart: unless-stopped
lb:
image: nginx:1.28.0-alpine@sha256:30f1c0d78e0ad60901648be663a710bdadf19e4c10ac6782c235200619158284
# 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/nginx/nginx.conf:/etc/nginx/nginx.conf:ro
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
restart: unless-stopped
volumes:
pgdata: