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.ymlat the repo root definespostgres,redis, a one-shotmigrate,api,worker,webandcaddy.docker compose up -d --buildruns everything;docker compose up -d postgres redisruns only the dependencies forpnpm dev.migraterunsprisma migrate deployonce, from the api's build stage (the only image with the Prisma CLI).apiandworkerwait for it to finish successfully.- Caddy is the only public entry point (
infra/Caddyfile)./api/*goes toapi:4000, everything else goes toweb: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_ADDRESSset to a real hostname makes Caddy obtain and renew the TLS certificate itself. - The api port is published on
127.0.0.1only. The api setstrust proxy = 'loopback, uniquelocal', so it trustsX-Forwarded-Forfrom Caddy's private address.req.ipis 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 ownX-Forwarded-Forto 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 shippnpm deploy --prodoutput; the web ships Next'sstandaloneoutput.
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.localfeeds both local dev and the containers. Compose itself reads.envfor its${VAR}substitutions, which trips people up;.env.exampleexplains 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.