Files
mtusi2026lab4/compose.yaml
T

198 lines
6.2 KiB
YAML

services:
postgres:
image: postgres:16.4-alpine
environment:
POSTGRES_USER: ${POSTGRES_USER:-messenger}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-changeme}
POSTGRES_DB: ${POSTGRES_DB:-messenger}
volumes:
- postgres_data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-messenger} -d ${POSTGRES_DB:-messenger}"]
interval: 5s
timeout: 3s
retries: 10
start_period: 5s
deploy:
resources:
limits:
cpus: "1.0"
memory: 512M
restart: unless-stopped
cache:
image: valkey/valkey:8.0.1-alpine
command: ["valkey-server", "--save", "", "--appendonly", "no"]
volumes:
- cache_data:/data
healthcheck:
test: ["CMD", "valkey-cli", "ping"]
interval: 5s
timeout: 3s
retries: 10
start_period: 3s
deploy:
resources:
limits:
cpus: "0.5"
memory: 256M
restart: unless-stopped
s3:
image: pgsty/silo:RELEASE.2026-09-16T00-00-00Z
command: server /data
environment:
MINIO_ROOT_USER: ${S3_ACCESS_KEY_ID:-minioadmin}
MINIO_ROOT_PASSWORD: ${S3_SECRET_ACCESS_KEY:-minioadmin}
volumes:
- minio_data:/data
healthcheck:
test: ["CMD", "silo", "healthcheck", "ready"]
interval: 5s
timeout: 3s
retries: 10
start_period: 5s
deploy:
resources:
limits:
cpus: "0.5"
memory: 512M
restart: unless-stopped
s3-init:
image: pgsty/mc:RELEASE.2026-09-16T00-00-00Z
depends_on:
s3:
condition: service_healthy
entrypoint: >
/bin/sh -c "
mc alias set local http://s3:9000 $${MINIO_ROOT_USER} $${MINIO_ROOT_PASSWORD} &&
mc mb --ignore-existing local/$${S3_BUCKET} &&
mc anonymous set none local/$${S3_BUCKET} &&
mc admin user add local $${S3_API_USER} $${S3_API_PASSWORD} 2>/dev/null || true &&
mc admin policy create local messenger-api-policy /tmp/api-policy.json 2>/dev/null || true &&
mc admin policy attach local messenger-api-policy --user $${S3_API_USER} 2>/dev/null || true &&
mc admin user add local $${S3_CLEANUP_USER} $${S3_CLEANUP_PASSWORD} 2>/dev/null || true &&
mc admin policy create local messenger-cleanup-policy /tmp/cleanup-policy.json 2>/dev/null || true &&
mc admin policy attach local messenger-cleanup-policy --user $${S3_CLEANUP_USER} 2>/dev/null || true &&
echo 'Bucket, API user, cleanup user ready: '$${S3_BUCKET}
"
volumes:
- ./infra/s3-policy.json:/tmp/api-policy.json:ro
- ./infra/s3-cleanup-policy.json:/tmp/cleanup-policy.json:ro
environment:
MINIO_ROOT_USER: ${S3_ACCESS_KEY_ID:-minioadmin}
MINIO_ROOT_PASSWORD: ${S3_SECRET_ACCESS_KEY:-minioadmin}
S3_BUCKET: ${S3_BUCKET:-messenger-attachments}
S3_API_USER: ${S3_API_USER:-messenger-api}
S3_API_PASSWORD: ${S3_API_PASSWORD:-messenger-api-secret}
S3_CLEANUP_USER: ${S3_CLEANUP_USER:-messenger-cleanup}
S3_CLEANUP_PASSWORD: ${S3_CLEANUP_PASSWORD:-messenger-cleanup-secret}
restart: "no"
migrate:
build:
context: .
dockerfile: Dockerfile
command: ["alembic", "upgrade", "head"]
depends_on:
postgres:
condition: service_healthy
environment:
DATABASE_URL: postgresql://${POSTGRES_USER:-messenger}:${POSTGRES_PASSWORD:-changeme}@postgres:5432/${POSTGRES_DB:-messenger}
deploy:
resources:
limits:
cpus: "0.5"
memory: 256M
restart: "no"
api:
build:
context: .
dockerfile: Dockerfile
command: ["python", "-m", "src.main"]
depends_on:
migrate:
condition: service_completed_successfully
cache:
condition: service_healthy
s3-init:
condition: service_completed_successfully
environment:
APP_HOST: 0.0.0.0
APP_PORT: 8080
APP_ENV: ${APP_ENV:-lab}
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
DATABASE_URL: postgresql://${POSTGRES_USER:-messenger}:${POSTGRES_PASSWORD:-changeme}@postgres:5432/${POSTGRES_DB:-messenger}
CACHE_URL: redis://cache:6379/0
S3_ENDPOINT: ${S3_ENDPOINT:-http://s3:9000}
S3_REGION: ${S3_REGION:-us-east-1}
S3_BUCKET: ${S3_BUCKET:-messenger-attachments}
S3_ACCESS_KEY_ID: ${S3_API_USER:-messenger-api}
S3_SECRET_ACCESS_KEY: ${S3_API_PASSWORD:-messenger-api-secret}
MAX_UPLOAD_BYTES: ${MAX_UPLOAD_BYTES:-10485760}
MAX_IMAGE_PIXELS: ${MAX_IMAGE_PIXELS:-40000000}
deploy:
replicas: 2
resources:
limits:
cpus: "0.5"
memory: 256M
healthcheck:
test: ["CMD", "python", "-c", "import urllib.request,sys; sys.exit(0 if urllib.request.urlopen('http://127.0.0.1:8080/health/live', timeout=2).status==200 else 1)"]
interval: 5s
timeout: 3s
retries: 5
start_period: 15s
restart: unless-stopped
lb:
image: haproxy:3.0-alpine
command: ["haproxy", "-W", "-db", "-f", "/usr/local/etc/haproxy/haproxy.cfg"]
volumes:
- ./infra/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
ports:
- "127.0.0.1:8080:8080"
depends_on:
api:
condition: service_healthy
deploy:
resources:
limits:
cpus: "0.25"
memory: 128M
restart: unless-stopped
cleanup:
build:
context: .
dockerfile: Dockerfile
command: ["python", "-m", "scripts.cleanup_orphans"]
depends_on:
postgres:
condition: service_healthy
s3:
condition: service_healthy
environment:
DATABASE_URL: postgresql://${POSTGRES_USER:-messenger}:${POSTGRES_PASSWORD:-changeme}@postgres:5432/${POSTGRES_DB:-messenger}
S3_ENDPOINT: ${S3_ENDPOINT:-http://s3:9000}
S3_REGION: ${S3_REGION:-us-east-1}
S3_BUCKET: ${S3_BUCKET:-messenger-attachments}
S3_ACCESS_KEY_ID: ${S3_CLEANUP_USER:-messenger-cleanup}
S3_SECRET_ACCESS_KEY: ${S3_CLEANUP_PASSWORD:-messenger-cleanup-secret}
CLEANUP_GRACE_HOURS: ${CLEANUP_GRACE_HOURS:-1}
deploy:
resources:
limits:
cpus: "0.25"
memory: 128M
restart: "no"
volumes:
postgres_data:
name: mtusi_lab4_postgres_data
cache_data:
name: mtusi_lab4_cache_data
minio_data:
name: mtusi_lab4_minio_data