name: messenger-lab7 x-lockdown: &lockdown security_opt: [no-new-privileges:true] cap_drop: [ALL] x-otel: &otel OTEL_EXPORTER_OTLP_ENDPOINT: ${OTEL_EXPORTER_OTLP_ENDPOINT:-http://otel-collector:4318} OTEL_EXPORTER_OTLP_PROTOCOL: http/protobuf OTEL_RESOURCE_ATTRIBUTES: ${OTEL_RESOURCE_ATTRIBUTES:-deployment.environment.name=lab} services: bootstrap: build: dockerfile_inline: | FROM python:3.12.7-alpine RUN apk add --no-cache openssl ENTRYPOINT ["python", "/work/bootstrap.py"] image: mtusi-messenger-lab7-bootstrap:dev network_mode: none security_opt: [no-new-privileges:true] cap_drop: [ALL] cap_add: [CHOWN, DAC_OVERRIDE, FOWNER] volumes: - ./infra/bootstrap.py:/work/bootstrap.py:ro - pki:/state deploy: resources: limits: {cpus: "1.0", memory: 96M} 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 broker rabbitmq relay worker grafana 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 broker_password:broker:24 admin_password:grafana:16; 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/* rabbitmq/* relay/* worker/* cp s3/s3_root_user s3/s3_root_password api/s3_access_key_id api/s3_secret_access_key s3-init/ db_url() { printf 'postgresql://messenger:%s@postgres:5432/messenger?connection_limit=%s&pool_timeout=5&connect_timeout=5&socket_timeout=10&application_name=%s&options=-c%%20statement_timeout%%3D5000' "$$(cat postgres/pg_password)" "$$1" "$$2"; } db_url 10 api > api/database_url db_url 5 worker > worker/database_url db_url 3 relay > relay/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 printf 'amqp://messenger:%s@rabbitmq:5672/' "$$(cat broker/broker_password)" > worker/broker_url cp worker/broker_url relay/ printf 'default_user = messenger\ndefault_pass = %s\nloopback_users = none\nlog.console = true\nlisteners.tcp.default = 5672\n' "$$(cat broker/broker_password)" > rabbitmq/10-messenger.conf cp api/database_url api/s3_access_key_id api/s3_secret_access_key reconciler/ cp api/s3_access_key_id api/s3_secret_access_key worker/ chmod 0444 postgres/* redis/* api/* migrate/* s3/* s3-init/* reconciler/* broker/* rabbitmq/* relay/* worker/* grafana/* deploy: resources: limits: { cpus: "0.1", memory: 32M } postgres: image: postgres:17.2-alpine environment: POSTGRES_USER: stand_operator POSTGRES_DB: messenger POSTGRES_PASSWORD_FILE: /run/tls/operator_password command: ["postgres", "-c", "ssl=on", "-c", "ssl_cert_file=/run/tls/postgres.crt", "-c", "ssl_key_file=/run/tls/postgres.key", "-c", "ssl_min_protocol_version=TLSv1.2", "-c", "hba_file=/etc/postgresql/pg_hba.conf"] security_opt: [no-new-privileges:true] volumes: - pgdata:/var/lib/postgresql/data - ./infra/postgres/init.sh:/docker-entrypoint-initdb.d/roles.sh:ro - ./infra/postgres/pg_hba.conf:/etc/postgresql/pg_hba.conf:ro - {type: volume, source: pki, target: /run/tls, read_only: true, volume: {subpath: postgres}} healthcheck: test: ["CMD", "pg_isready", "-d", "host=postgres dbname=messenger user=migrate sslmode=verify-full sslrootcert=/run/tls/ca.crt"] interval: 3s timeout: 3s retries: 15 depends_on: bootstrap: {condition: service_completed_successfully} restart: unless-stopped deploy: resources: limits: {cpus: "1.0", memory: 512M} 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: <<: *lockdown image: pgsty/minio:RELEASE.2026-08-04T00-00-00Z@sha256:b6bfe7239bfc83fb90d31612d9704d86039dd714f7904b3f1ad68f211e602372 command: ["server", "/data", "--address", ":9000", "--console-address", "127.0.0.1:9001", "--certs-dir", "/run/tls"] environment: MINIO_ROOT_USER_FILE: /run/tls/root_user MINIO_ROOT_PASSWORD_FILE: /run/tls/root_password MINIO_BROWSER: "off" volumes: - s3data:/data - {type: volume, source: pki, target: /run/tls, read_only: true, volume: {subpath: s3}} healthcheck: test: ["CMD", "curl", "-fsS", "--cacert", "/run/tls/ca.crt", "https://s3:9000/minio/health/live"] interval: 3s timeout: 3s retries: 15 depends_on: bootstrap: {condition: service_completed_successfully} restart: unless-stopped deploy: resources: limits: {cpus: "0.5", memory: 512M} s3-init: <<: *lockdown image: pgsty/mc:RELEASE.2026-10-26T00-00-00Z@sha256:ce62c9053c69ce12a527a322345b5ad9f44b5f8a7b8189f82e516f2e3f30f1d8 entrypoint: ["sh", "/work/init.sh"] environment: S3_BUCKET: ${S3_BUCKET:-messenger-attachments} volumes: - ./infra/s3/init.sh:/work/init.sh:ro - {type: volume, source: pki, target: /run/tls, read_only: true, volume: {subpath: s3-init}} depends_on: s3: {condition: service_healthy} deploy: resources: limits: {cpus: "0.25", memory: 192M} migrate: <<: *lockdown build: context: . target: migrate image: mtusi-messenger-lab7-migrate:dev volumes: - {type: volume, source: pki, target: /run/tls, read_only: true, volume: {subpath: migrate}} depends_on: postgres: {condition: service_healthy} deploy: resources: limits: {cpus: "0.5", memory: 512M} api: build: context: . target: runtime image: mtusi-messenger-lab7-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/tls/database_url CACHE_URL_FILE: /run/secrets/cache_url NODE_EXTRA_CA_CERTS: /run/tls/ca.crt S3_ENDPOINT: https://s3:9000 S3_REGION: ${S3_REGION:-us-east-1} S3_BUCKET: ${S3_BUCKET:-messenger-attachments} S3_ACCESS_KEY_ID_FILE: /run/tls/s3_access_key_id S3_SECRET_ACCESS_KEY_FILE: /run/tls/s3_secret_access_key MAX_UPLOAD_BYTES: ${MAX_UPLOAD_BYTES:-10485760} MAX_IMAGE_PIXELS: ${MAX_IMAGE_PIXELS:-40000000} JOB_MAX_ATTEMPTS: ${JOB_MAX_ATTEMPTS:-5} <<: *otel OTEL_SERVICE_NAME: messenger-api volumes: - { type: volume, source: secrets, target: /run/secrets, read_only: true, volume: { subpath: api } } - { type: volume, source: pki, target: /run/tls, read_only: true, volume: { subpath: api } } deploy: replicas: ${API_REPLICAS:-2} resources: limits: { cpus: "0.5", memory: 256M } stop_grace_period: 10s depends_on: bootstrap: condition: service_completed_successfully 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-lab7-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/tls/database_url STAGING_RETENTION_SECONDS: ${STAGING_RETENTION_SECONDS:-86400} NODE_EXTRA_CA_CERTS: /run/tls/ca.crt S3_ENDPOINT: https://s3:9000 S3_REGION: ${S3_REGION:-us-east-1} S3_BUCKET: ${S3_BUCKET:-messenger-attachments} S3_ACCESS_KEY_ID_FILE: /run/tls/s3_access_key_id S3_SECRET_ACCESS_KEY_FILE: /run/tls/s3_secret_access_key volumes: - { type: volume, source: secrets, target: /run/secrets, read_only: true, volume: { subpath: reconciler } } - { type: volume, source: pki, target: /run/tls, 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 rabbitmq: image: rabbitmq:4.0.5-alpine@sha256:e045b5166b9188a38eee5c99411709aa1f0ffa2ac75664053e64cb69411f7c32 hostname: rabbitmq security_opt: [no-new-privileges:true] environment: RABBITMQ_NODENAME: rabbit@localhost ERL_EPMD_ADDRESS: 127.0.0.1 volumes: - rabbitmqdata:/var/lib/rabbitmq - ./infra/rabbitmq/10-messenger.conf:/etc/rabbitmq/conf.d/10-messenger.conf:ro - ./infra/rabbitmq/enabled_plugins:/etc/rabbitmq/enabled_plugins:ro - {type: volume, source: pki, target: /run/tls, read_only: true, volume: {subpath: rabbitmq}} healthcheck: test: ["CMD", "rabbitmq-diagnostics", "-q", "check_port_connectivity"] interval: 5s timeout: 8s start_period: 20s retries: 12 depends_on: bootstrap: {condition: service_completed_successfully} stop_grace_period: 30s restart: unless-stopped deploy: resources: limits: {cpus: "0.5", memory: 512M} relay: build: context: . target: runtime image: mtusi-messenger-lab7-api:dev command: ["node", "dist/relay.js"] environment: NODE_EXTRA_CA_CERTS: /run/tls/ca.crt DATABASE_URL_FILE: /run/tls/database_url BROKER_URL_FILE: /run/tls/broker_url AMQP_TOPOLOGY_PREPROVISIONED: "true" JOB_STALE_SECONDS: ${JOB_STALE_SECONDS:-300} RELAY_FAULT: ${RELAY_FAULT:-} <<: *otel OTEL_SERVICE_NAME: messenger-relay volumes: - { type: volume, source: secrets, target: /run/secrets, read_only: true, volume: { subpath: relay } } - { type: volume, source: pki, target: /run/tls, read_only: true, volume: { subpath: relay } } deploy: resources: limits: { cpus: "0.25", memory: 128M } healthcheck: disable: true stop_grace_period: 10s depends_on: migrate: condition: service_completed_successfully rabbitmq: condition: service_healthy restart: unless-stopped worker: build: context: . target: runtime image: mtusi-messenger-lab7-api:dev command: ["node", "dist/worker.js"] environment: DATABASE_URL_FILE: /run/tls/database_url BROKER_URL_FILE: /run/tls/broker_url AMQP_TOPOLOGY_PREPROVISIONED: "true" NODE_EXTRA_CA_CERTS: /run/tls/ca.crt S3_ENDPOINT: https://s3:9000 S3_REGION: ${S3_REGION:-us-east-1} S3_BUCKET: ${S3_BUCKET:-messenger-attachments} S3_ACCESS_KEY_ID_FILE: /run/tls/s3_access_key_id S3_SECRET_ACCESS_KEY_FILE: /run/tls/s3_secret_access_key MAX_UPLOAD_BYTES: ${MAX_UPLOAD_BYTES:-10485760} MAX_IMAGE_PIXELS: ${MAX_IMAGE_PIXELS:-40000000} WORKER_CONCURRENCY: ${WORKER_CONCURRENCY:-2} JOB_TIMEOUT_SECONDS: ${JOB_TIMEOUT_SECONDS:-60} JOB_LEASE_SECONDS: ${JOB_LEASE_SECONDS:-90} WORKER_FAULT: ${WORKER_FAULT:-} <<: *otel OTEL_SERVICE_NAME: messenger-worker volumes: - { type: volume, source: secrets, target: /run/secrets, read_only: true, volume: { subpath: worker } } - { type: volume, source: pki, target: /run/tls, read_only: true, volume: { subpath: worker } } deploy: resources: limits: { cpus: "1.0", memory: 768M } healthcheck: disable: true stop_grace_period: 30s depends_on: migrate: condition: service_completed_successfully rabbitmq: condition: service_healthy s3-init: condition: service_completed_successfully restart: unless-stopped lb: <<: *lockdown image: haproxy:3.0.6-alpine user: "1000:1000" ports: - "127.0.0.1:${HTTPS_PORT:-8443}:8443" volumes: - ./infra/haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro - {type: volume, source: pki, target: /run/tls, read_only: true, volume: {subpath: lb}} healthcheck: test: ["CMD", "wget", "-q", "-O", "/dev/null", "http://127.0.0.1:8404/"] interval: 3s timeout: 2s retries: 20 depends_on: api: {condition: service_started} restart: unless-stopped deploy: resources: limits: {cpus: "0.25", memory: 96M} otel-collector: image: otel/opentelemetry-collector-contrib:0.115.1@sha256:d2da12c4336a79758826700be9e21ecf4a9f7d945b7f8a58ba55ee3fa45427c8 command: ["--config=/etc/otelcol/collector.yaml"] volumes: - ./infra/otel/collector.yaml:/etc/otelcol/collector.yaml:ro networks: [default, telemetry] deploy: resources: limits: { cpus: "0.5", memory: 256M } restart: unless-stopped prometheus: image: prom/prometheus:v3.1.0@sha256:6559acbd5d770b15bb3c954629ce190ac3cbbdb2b7f1c30f0385c4e05104e218 command: - --config.file=/etc/prometheus/prometheus.yml - --storage.tsdb.path=/prometheus - --storage.tsdb.retention.time=7d - --storage.tsdb.retention.size=1GB - --web.enable-otlp-receiver volumes: - ./infra/prometheus:/etc/prometheus:ro - prometheusdata:/prometheus networks: [telemetry] deploy: resources: limits: { cpus: "0.5", memory: 512M } healthcheck: test: ["CMD", "wget", "-q", "-O", "/dev/null", "http://127.0.0.1:9090/-/ready"] interval: 5s timeout: 3s retries: 20 restart: unless-stopped loki: image: grafana/loki:3.3.2@sha256:8af2de1abbdd7aa92b27c9bcc96f0f4140c9096b507c77921ffddf1c6ad6c48f command: ["-config.file=/etc/loki/loki.yaml"] volumes: - ./infra/loki/loki.yaml:/etc/loki/loki.yaml:ro - lokidata:/loki networks: [telemetry] deploy: resources: limits: { cpus: "0.5", memory: 512M } restart: unless-stopped tempo: image: grafana/tempo:2.6.1@sha256:ef4384fce6e8ad22b95b243d8fc165628cda655376fd50e7850536ad89d71d50 command: ["-config.file=/etc/tempo/tempo.yaml"] volumes: - ./infra/tempo/tempo.yaml:/etc/tempo/tempo.yaml:ro - tempodata:/var/tempo networks: [telemetry] deploy: resources: limits: { cpus: "0.5", memory: 512M } restart: unless-stopped grafana: image: grafana/grafana:11.4.0@sha256:d8ea37798ccc41061a62ab080f2676dda6bf7815558499f901bdb0f533a456fb ports: - "127.0.0.1:${GRAFANA_PORT:-3000}:3000" environment: GF_SECURITY_ADMIN_USER: admin GF_SECURITY_ADMIN_PASSWORD__FILE: /run/secrets/admin_password GF_AUTH_ANONYMOUS_ENABLED: "true" GF_AUTH_ANONYMOUS_ORG_ROLE: Viewer GF_USERS_VIEWERS_CAN_EDIT: "true" GF_ANALYTICS_REPORTING_ENABLED: "false" GF_ANALYTICS_CHECK_FOR_UPDATES: "false" GF_NEWS_NEWS_FEED_ENABLED: "false" volumes: - ./infra/grafana/provisioning:/etc/grafana/provisioning:ro - ./infra/grafana/dashboards:/var/lib/grafana/dashboards:ro - grafanadata:/var/lib/grafana - { type: volume, source: secrets, target: /run/secrets, read_only: true, volume: { subpath: grafana } } networks: [default, telemetry] deploy: resources: limits: { cpus: "0.5", memory: 256M } healthcheck: test: ["CMD", "wget", "-q", "-O", "/dev/null", "http://127.0.0.1:3000/api/health"] interval: 5s timeout: 3s retries: 20 depends_on: secrets-init: condition: service_completed_successfully prometheus: condition: service_healthy loki: condition: service_started tempo: condition: service_started restart: unless-stopped checks: <<: *lockdown profiles: [tools] image: mtusi-messenger-lab7-bootstrap:dev entrypoint: [python] working_dir: /work volumes: - .:/work:ro - {type: volume, source: pki, target: /run/tls, read_only: true, volume: {subpath: checks}} depends_on: bootstrap: {condition: service_completed_successfully} deploy: resources: limits: {cpus: "0.5", memory: 128M} volumes: pgdata: s3data: rabbitmqdata: prometheusdata: lokidata: tempodata: grafanadata: secrets: pki: networks: telemetry: internal: true