Files
mtusi2026lab4/compose.yaml
T

241 lines
8.6 KiB
YAML

name: messenger-lab4
services:
secrets-init:
image: alpine:3.20.3
volumes:
- secrets:/secrets
command:
- sh
- -ec
- |
cd /secrets
mkdir -p postgres redis api migrate s3 s3-init reconciler
for entry in pg_password:postgres:24 redis_password:redis:24 s3_root_user:s3:10 s3_root_password:s3:20 s3_access_key_id:api:10 s3_secret_access_key:api:20; do
file=$${entry%%:*}; rest=$${entry#*:}; dir=$${rest%%:*}; size=$${rest##*:}
[ -s "$$dir/$$file" ] || head -c "$$size" /dev/urandom | od -An -tx1 | tr -d ' \n' > "$$dir/$$file"
done
rm -f s3-init/*
cp s3/s3_root_user s3/s3_root_password api/s3_access_key_id api/s3_secret_access_key s3-init/
printf 'postgresql://messenger:%s@postgres:5432/messenger?connection_limit=10&pool_timeout=5&connect_timeout=5&socket_timeout=10&application_name=api&options=-c%%20statement_timeout%%3D5000' "$$(cat postgres/pg_password)" > api/database_url
printf 'redis://:%s@redis:6379/0' "$$(cat redis/redis_password)" > api/cache_url
printf 'postgresql://messenger:%s@postgres:5432/messenger' "$$(cat postgres/pg_password)" > migrate/database_url
cp api/database_url api/s3_access_key_id api/s3_secret_access_key reconciler/
chmod 0444 postgres/* redis/* api/* migrate/* s3/* s3-init/* reconciler/*
deploy:
resources:
limits: { cpus: "0.1", memory: 32M }
postgres:
image: postgres:17.2-alpine
environment:
POSTGRES_USER: messenger
POSTGRES_DB: messenger
POSTGRES_PASSWORD_FILE: /run/secrets/pg_password
volumes:
- pgdata:/var/lib/postgresql/data
- { type: volume, source: secrets, target: /run/secrets, read_only: true, volume: { subpath: postgres } }
deploy:
resources:
limits: { cpus: "1.0", memory: 512M }
healthcheck:
test: ["CMD", "pg_isready", "-U", "messenger", "-d", "messenger"]
interval: 5s
timeout: 3s
retries: 10
depends_on:
secrets-init:
condition: service_completed_successfully
restart: unless-stopped
redis:
image: redis:7.4.1-alpine
user: redis
command:
- sh
- -c
- exec redis-server --save '' --appendonly no --maxmemory 64mb --maxmemory-policy allkeys-lru --requirepass "$$(cat /run/secrets/redis_password)"
volumes:
- { type: volume, source: secrets, target: /run/secrets, read_only: true, volume: { subpath: redis } }
deploy:
resources:
limits: { cpus: "0.25", memory: 128M }
healthcheck:
test: ["CMD-SHELL", "redis-cli --no-auth-warning -a \"$$(cat /run/secrets/redis_password)\" ping | grep -q PONG"]
interval: 5s
timeout: 3s
retries: 10
depends_on:
secrets-init:
condition: service_completed_successfully
restart: unless-stopped
s3:
image: pgsty/minio:RELEASE.2026-08-04T00-00-00Z@sha256:b6bfe7239bfc83fb90d31612d9704d86039dd714f7904b3f1ad68f211e602372
command: ["server", "/data", "--address", ":9000"]
environment:
MINIO_ROOT_USER_FILE: /run/secrets/s3_root_user
MINIO_ROOT_PASSWORD_FILE: /run/secrets/s3_root_password
MINIO_BROWSER: "off"
volumes:
- s3data:/data
- { type: volume, source: secrets, target: /run/secrets, read_only: true, volume: { subpath: s3 } }
deploy:
resources:
limits: { cpus: "0.5", memory: 512M }
healthcheck:
test: ["CMD", "curl", "-fsS", "-o", "/dev/null", "http://127.0.0.1:9000/minio/health/live"]
interval: 5s
timeout: 3s
retries: 10
depends_on:
secrets-init:
condition: service_completed_successfully
restart: unless-stopped
s3-init:
image: pgsty/mc:RELEASE.2026-10-26T00-00-00Z@sha256:ce62c9053c69ce12a527a322345b5ad9f44b5f8a7b8189f82e516f2e3f30f1d8
entrypoint: ["sh", "-ec"]
command:
- |
cd /run/secrets
export MC_HOST_root="http://$$(cat s3_root_user):$$(cat s3_root_password)@s3:9000"
mc mb --ignore-existing "root/$$S3_BUCKET"
mc anonymous set none "root/$$S3_BUCKET"
cat > /tmp/policy.json <<POLICY
{"Version":"2012-10-17","Statement":[
{"Effect":"Allow","Action":["s3:ListBucket","s3:GetBucketLocation","s3:ListBucketMultipartUploads"],"Resource":["arn:aws:s3:::$$S3_BUCKET"]},
{"Effect":"Allow","Action":["s3:GetObject","s3:PutObject","s3:DeleteObject","s3:AbortMultipartUpload","s3:ListMultipartUploadParts"],"Resource":["arn:aws:s3:::$$S3_BUCKET/*"]}
]}
POLICY
mc admin policy create root messenger-api /tmp/policy.json
mc admin user add root "$$(cat s3_access_key_id)" "$$(cat s3_secret_access_key)" > /dev/null
mc admin policy attach root messenger-api --user "$$(cat s3_access_key_id)" > /dev/null
echo "bucket $$S3_BUCKET ready"
environment:
S3_BUCKET: ${S3_BUCKET:-messenger-attachments}
volumes:
- { type: volume, source: secrets, target: /run/secrets, read_only: true, volume: { subpath: s3-init } }
deploy:
resources:
limits: { cpus: "0.25", memory: 256M }
depends_on:
s3:
condition: service_healthy
migrate:
build:
context: .
target: migrate
image: mtusi-messenger-lab4-migrate:dev
environment:
DATABASE_URL_FILE: /run/secrets/database_url
volumes:
- { type: volume, source: secrets, target: /run/secrets, read_only: true, volume: { subpath: migrate } }
deploy:
resources:
limits: { cpus: "0.5", memory: 512M }
depends_on:
postgres:
condition: service_healthy
api:
build:
context: .
target: runtime
image: mtusi-messenger-lab4-api:dev
environment:
APP_HOST: 0.0.0.0
APP_PORT: 8080
APP_ENV: ${APP_ENV:-lab}
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
IDEMPOTENCY_TTL_SECONDS: ${IDEMPOTENCY_TTL_SECONDS:-86400}
SHUTDOWN_DELAY_MS: 3000
DATABASE_URL_FILE: /run/secrets/database_url
CACHE_URL_FILE: /run/secrets/cache_url
S3_ENDPOINT: http://s3:9000
S3_REGION: ${S3_REGION:-us-east-1}
S3_BUCKET: ${S3_BUCKET:-messenger-attachments}
S3_ACCESS_KEY_ID_FILE: /run/secrets/s3_access_key_id
S3_SECRET_ACCESS_KEY_FILE: /run/secrets/s3_secret_access_key
MAX_UPLOAD_BYTES: ${MAX_UPLOAD_BYTES:-10485760}
MAX_IMAGE_PIXELS: ${MAX_IMAGE_PIXELS:-40000000}
volumes:
- { type: volume, source: secrets, target: /run/secrets, read_only: true, volume: { subpath: api } }
deploy:
replicas: ${API_REPLICAS:-2}
resources:
limits: { cpus: "0.5", memory: 256M }
stop_grace_period: 10s
depends_on:
migrate:
condition: service_completed_successfully
redis:
condition: service_started
s3-init:
condition: service_completed_successfully
restart: unless-stopped
reconciler:
build:
context: .
target: runtime
image: mtusi-messenger-lab4-api:dev
command: ["node", "dist/reconcile.js", "--apply", "--loop", "--grace-seconds=${RECONCILE_GRACE_SECONDS:-3600}", "--interval-seconds=${RECONCILE_INTERVAL_SECONDS:-300}"]
environment:
DATABASE_URL_FILE: /run/secrets/database_url
S3_ENDPOINT: http://s3:9000
S3_REGION: ${S3_REGION:-us-east-1}
S3_BUCKET: ${S3_BUCKET:-messenger-attachments}
S3_ACCESS_KEY_ID_FILE: /run/secrets/s3_access_key_id
S3_SECRET_ACCESS_KEY_FILE: /run/secrets/s3_secret_access_key
volumes:
- { type: volume, source: secrets, target: /run/secrets, read_only: true, volume: { subpath: reconciler } }
deploy:
resources:
limits: { cpus: "0.25", memory: 128M }
healthcheck:
disable: true
depends_on:
migrate:
condition: service_completed_successfully
s3-init:
condition: service_completed_successfully
restart: unless-stopped
lb:
image: haproxy:3.0.6-alpine
ports:
- "127.0.0.1:${APP_PORT:-8080}:8080"
volumes:
- ./infra/haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
deploy:
resources:
limits: { cpus: "0.25", memory: 64M }
depends_on:
api:
condition: service_healthy
restart: unless-stopped
tools:
profiles: [tools]
build:
dockerfile_inline: |
FROM docker:27.3.1-cli
RUN apk add --no-cache python3 make git && git config --global --add safe.directory /work
WORKDIR /work
image: mtusi-messenger-lab4-tools:dev
environment:
BASE_URL: http://lb:8080
volumes:
- .:/work
- /var/run/docker.sock:/var/run/docker.sock
deploy:
resources:
limits: { cpus: "1.0", memory: 256M }
volumes:
pgdata:
s3data:
secrets: