# Lab4 local teaching stand: client -> HAProxy -> 2+ interchangeable API # processes -> shared PostgreSQL and Valkey, plus a private SeaweedFS S3 store. # # There is intentionally no container_name: a Compose service with fixed # container names cannot be scaled. Every replica gets its own container ID, # hostname and X-Instance-Id. Only HAProxy on 127.0.0.1:8084 is published; # API, S3, SeaweedFS master/filer/volume and the admin surfaces stay on the # internal backend network. # # Start: make env && make up # Stop: make stop (processes stopped, named volumes and data preserved) # Remove: make down (containers removed, volumes preserved) # Purge: make purge (containers AND named volumes removed; explicit data loss) name: ${COMPOSE_PROJECT_NAME:-mtusi-lab4-melnikov} x-api-build: &api-build context: . args: VERSION: ${APP_VERSION:-1.0.0} REVISION: ${GIT_REVISION:-unknown} x-s3-common: &s3-common S3_ENDPOINT: http://s3:8333 S3_REGION: ${S3_REGION:-us-east-1} S3_BUCKET: ${S3_BUCKET:-messenger-attachments} S3_TIMEOUT_SECONDS: ${S3_TIMEOUT_SECONDS:-10} services: postgres: image: postgres:16.4-alpine3.20@sha256:5660c2cbfea50c7a9127d17dc4e48543eedd3d7a41a595a2dfa572471e37e64c environment: POSTGRES_USER: ${POSTGRES_USER:-messenger} POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?set POSTGRES_PASSWORD in .env via make env} POSTGRES_DB: ${POSTGRES_DB:-messenger} PGDATA: /var/lib/postgresql/data/pgdata command: - postgres - -c - max_connections=100 - -c - shared_buffers=128MB - -c - effective_cache_size=256MB - -c - work_mem=4MB - -c - maintenance_work_mem=64MB - -c - max_wal_size=4GB - -c - checkpoint_timeout=30min - -c - checkpoint_completion_target=0.9 volumes: - pgdata:/var/lib/postgresql/data networks: - backend healthcheck: test: ["CMD-SHELL", "pg_isready -U $${POSTGRES_USER} -d $${POSTGRES_DB}"] interval: 3s timeout: 3s retries: 20 start_period: 5s restart: unless-stopped security_opt: - no-new-privileges:true stop_grace_period: 20s deploy: resources: limits: cpus: "${POSTGRES_CPU_LIMIT:-0.5}" memory: ${POSTGRES_MEM_LIMIT:-384M} valkey: image: valkey/valkey:8.0.2-alpine@sha256:752ba000a58bc8925d11ee863a39b5225617cd2cb3dbc9ec8ac65268b65ebb6d environment: VALKEY_PASSWORD: ${VALKEY_PASSWORD:?set VALKEY_PASSWORD in .env via make env} # Shared session store. AOF everysec is the durability boundary: graceful # restart keeps acknowledged sessions; a hard crash can lose up to one # second of session writes. TTLs are absolute deadlines, so replaying the # AOF neither revives nor extends a session. command: - valkey-server - --appendonly - "yes" - --appendfsync - everysec - --requirepass - ${VALKEY_PASSWORD:?set VALKEY_PASSWORD in .env via make env} - --protected-mode - "no" volumes: - valkeydata:/data networks: - backend healthcheck: test: ["CMD-SHELL", "valkey-cli --no-auth-warning -a \"$$VALKEY_PASSWORD\" ping | grep -q PONG"] interval: 3s timeout: 3s retries: 20 start_period: 5s restart: unless-stopped security_opt: - no-new-privileges:true stop_grace_period: 20s deploy: resources: limits: cpus: "${VALKEY_CPU_LIMIT:-0.1}" memory: ${VALKEY_MEM_LIMIT:-96M} # One S3-compatible server (SeaweedFS) owns the original bytes. The image is # pinned by tag and digest. The master/filer/volume/S3 ports stay unpublished; # only the API reaches the private bucket over the backend network. s3: image: chrislusf/seaweedfs:3.98@sha256:2f7de737bd35c5e2cf93944e8aa66de802a135f068ea285d8d290d0206347d22 command: - server - -dir=/data - -s3 - -s3.port=8333 - -s3.config=/etc/seaweedfs/s3.json volumes: - s3data:/data # Generated locally by make env from .env; contains local synthetic # credentials only and is ignored by Git. - ./infra/seaweedfs-s3.json:/etc/seaweedfs/s3.json:ro networks: - backend healthcheck: # The master answers on 9333 before the S3 gateway on 8333 is ready. # Probe the actual S3 port: any HTTP response (normally 403 for an # anonymous root request) proves the gateway is listening. test: ["CMD-SHELL", "wget -S -O /dev/null http://127.0.0.1:8333/ 2>&1 | grep -qE 'HTTP/[0-9.]+ (200|400|403)'"] interval: 3s timeout: 3s retries: 30 start_period: 20s restart: unless-stopped security_opt: - no-new-privileges:true stop_grace_period: 20s deploy: resources: limits: cpus: "${S3_CPU_LIMIT:-0.4}" memory: ${S3_MEM_LIMIT:-384M} # One-shot, idempotent bucket bootstrap with the administrative SeaweedFS # identity. The API never receives these credentials. s3-init: image: ${API_IMAGE:-mtusi-messenger-lab4-melnikov:local} build: *api-build entrypoint: ["/app/s3bootstrap"] environment: <<: *s3-common S3_ADMIN_ACCESS_KEY: ${S3_ADMIN_ACCESS_KEY:?set S3_ADMIN_ACCESS_KEY in .env via make env} S3_ADMIN_SECRET_KEY: ${S3_ADMIN_SECRET_KEY:?set S3_ADMIN_SECRET_KEY in .env via make env} depends_on: s3: condition: service_healthy networks: - backend # A stopped service alias must fail closed. Without an explicit search # domain, the host's public search suffixes could turn the single-label # name "s3" into a public host when the container is temporarily absent. # "invalid" is reserved by RFC 2606 and always NXDOMAIN. dns_search: - invalid restart: "no" read_only: true tmpfs: - /tmp security_opt: - no-new-privileges:true deploy: resources: limits: cpus: "0.2" memory: 64M # One-shot migration runner. API replicas depend on this service completing, # so the schema is never migrated by a race between replicas. The advisory # lock and checksums make a repeated invocation safe. migrate: image: ${API_IMAGE:-mtusi-messenger-lab4-melnikov:local} build: *api-build entrypoint: ["/app/migrate"] environment: APP_ENV: ${APP_ENV:-lab} LOG_LEVEL: ${LOG_LEVEL:-info} DATABASE_URL: postgres://${POSTGRES_USER:-messenger}:${POSTGRES_PASSWORD:?set POSTGRES_PASSWORD in .env via make env}@postgres:5432/${POSTGRES_DB:-messenger}?sslmode=disable DB_MAX_CONNS: "2" DB_CONNECT_TIMEOUT_SECONDS: "3" DB_STATEMENT_TIMEOUT_SECONDS: "10" DB_QUERY_TIMEOUT_SECONDS: "10" depends_on: postgres: condition: service_healthy networks: - backend restart: "no" read_only: true security_opt: - no-new-privileges:true deploy: resources: limits: cpus: "0.25" memory: 128M api: image: ${API_IMAGE:-mtusi-messenger-lab4-melnikov:local} build: *api-build environment: APP_HOST: 0.0.0.0 APP_PORT: "8080" APP_ENV: ${APP_ENV:-lab} LOG_LEVEL: ${LOG_LEVEL:-info} DATABASE_URL: postgres://${POSTGRES_USER:-messenger}:${POSTGRES_PASSWORD:?set POSTGRES_PASSWORD in .env via make env}@postgres:5432/${POSTGRES_DB:-messenger}?sslmode=disable CACHE_URL: redis://:${VALKEY_PASSWORD:?set VALKEY_PASSWORD in .env via make env}@valkey:6379/0 SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600} DB_MAX_CONNS: ${DB_MAX_CONNS:-8} DB_MIN_CONNS: ${DB_MIN_CONNS:-1} DB_CONNECT_TIMEOUT_SECONDS: ${DB_CONNECT_TIMEOUT_SECONDS:-3} DB_STATEMENT_TIMEOUT_SECONDS: ${DB_STATEMENT_TIMEOUT_SECONDS:-3} DB_QUERY_TIMEOUT_SECONDS: ${DB_QUERY_TIMEOUT_SECONDS:-3} CACHE_POOL_SIZE: ${CACHE_POOL_SIZE:-8} CACHE_TIMEOUT_SECONDS: ${CACHE_TIMEOUT_SECONDS:-2} SHUTDOWN_TIMEOUT_SECONDS: ${SHUTDOWN_TIMEOUT_SECONDS:-6} SHUTDOWN_DRAIN_SECONDS: ${SHUTDOWN_DRAIN_SECONDS:-3} STARTUP_TIMEOUT_SECONDS: ${STARTUP_TIMEOUT_SECONDS:-30} METRICS_ADDR: 127.0.0.1:9090 GOMEMLIMIT: ${GOMEMLIMIT:-300MiB} <<: *s3-common S3_ACCESS_KEY: ${S3_API_ACCESS_KEY:?set S3_API_ACCESS_KEY in .env via make env} S3_SECRET_KEY: ${S3_API_SECRET_KEY:?set S3_API_SECRET_KEY in .env via make env} MAX_UPLOAD_BYTES: ${MAX_UPLOAD_BYTES:-10485760} MAX_IMAGE_PIXELS: ${MAX_IMAGE_PIXELS:-40000000} UPLOAD_DECODE_CONCURRENCY: ${UPLOAD_DECODE_CONCURRENCY:-1} S3_PUT_CONCURRENCY: ${S3_PUT_CONCURRENCY:-1} depends_on: postgres: condition: service_healthy valkey: condition: service_healthy migrate: condition: service_completed_successfully s3-init: condition: service_completed_successfully networks: - backend # Same fail-closed DNS rule as s3-init: while S3 is stopped, "s3" must not # fall through to a public search-domain host. dns_search: - invalid healthcheck: test: ["CMD", "/app/messenger", "-healthcheck"] interval: 3s timeout: 2s retries: 12 start_period: 10s restart: unless-stopped read_only: true tmpfs: # Bounded scratch space for one upload in flight. It is an ephemeral # staging area, never the source of truth for a ready attachment. - /tmp:size=256m,mode=1777 security_opt: - no-new-privileges:true cap_drop: - ALL stop_grace_period: 15s deploy: replicas: ${API_REPLICAS:-2} resources: limits: cpus: "${API_CPU_LIMIT:-0.5}" memory: ${API_MEM_LIMIT:-384M} lb: image: haproxy:3.0-alpine@sha256:457d1c0dcbf2dfc0e0358723c07df4410d30e7de42bec036b383cfede4a5dbe3 volumes: - ./infra/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro ports: - "127.0.0.1:${LB_PORT:-8084}:8080" networks: - backend healthcheck: test: ["CMD-SHELL", "wget -q -O /dev/null http://127.0.0.1:8404/stats || exit 1"] interval: 3s timeout: 2s retries: 10 start_period: 5s restart: unless-stopped read_only: true tmpfs: - /tmp security_opt: - no-new-privileges:true cap_drop: - ALL stop_grace_period: 10s deploy: resources: limits: cpus: "${LB_CPU_LIMIT:-0.2}" memory: ${LB_MEM_LIMIT:-64M} volumes: pgdata: name: ${COMPOSE_PROJECT_NAME:-mtusi-lab4-melnikov}-pgdata valkeydata: name: ${COMPOSE_PROJECT_NAME:-mtusi-lab4-melnikov}-valkeydata s3data: name: ${COMPOSE_PROJECT_NAME:-mtusi-lab4-melnikov}-s3data networks: backend: name: ${COMPOSE_PROJECT_NAME:-mtusi-lab4-melnikov}-net