Bỏ qua, tới nội dung chính

Kiến trúc · ADR 0006

Docker Compose behind Caddy, one origin

Trạng thái
accepted
Ngày
2026-09-21
Người quyết định
Alex

Context

The system is five processes: web, api, worker, Postgres and Redis. The demo and the final defence run on a single machine, so there is no need for an orchestrator. What matters is that the whole thing starts with one command, and that the browser does not need CORS or cross-site cookies.

Decision

  • docker-compose.yml at the repo root defines postgres, redis, a one-shot migrate, api, worker, web and caddy. docker compose up -d --build runs everything; docker compose up -d postgres redis runs only the dependencies for pnpm dev.
  • migrate runs prisma migrate deploy once, from the api's build stage (the only image with the Prisma CLI). api and worker wait for it to finish successfully.
  • Caddy is the only public entry point (infra/Caddyfile). /api/* goes to api:4000, everything else goes to web:8080 (the same number Caddy publishes, so the "localhost:8080" link Next prints in the container log opens this app, not whatever else runs on the host's 3000), so browser and API share one origin. SITE_ADDRESS set to a real hostname makes Caddy obtain and renew the TLS certificate itself.
  • The api port is published on 127.0.0.1 only. The api sets trust proxy = 'loopback, uniquelocal', so it trusts X-Forwarded-For from Caddy's private address. req.ip is then the real client, which the rate limiter keys on. Published on all interfaces, the api would be reachable from other machines on the LAN, which are private addresses too, and they could set their own X-Forwarded-For to dodge the limiter.
  • Host ports avoid the defaults: Postgres on 5433, Redis on 6380, Caddy on 8080/8443. A native Postgres on 5432 would otherwise answer instead of the container, and the error would look like wrong credentials. All are overridable through the root .env.
  • Images are multi-stage node:22-alpine. The api and worker ship pnpm deploy --prod output; the web ships Next's standalone output.

Alternatives considered

  • nginx. Needs certbot or manual certificates. Caddy handles TLS on its own.
  • Separate origins with CORS. Works, but every auth decision (cookies, preflights) gets harder.
  • Kubernetes / a PaaS per service. More than one machine needs.

Consequences

  • One .env.local feeds both local dev and the containers. Compose itself reads .env for its ${VAR} substitutions, which trips people up; .env.example explains it.
  • In production, CORS is strict (CORS_ORIGINS) even though same-origin traffic does not need it.
  • The three images total about 1.2 GB.