From 815dcc300d32b12433186e15dba675b0317b296e Mon Sep 17 00:00:00 2001 From: Jose Selesan Date: Mon, 19 Oct 2026 12:05:20 -0300 Subject: [PATCH] =?UTF-8?q?fix(deploy):=20isolate=20web=E2=86=92api=20traf?= =?UTF-8?q?fic=20on=20a=20private=20network?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit nginx proxied to `api:4100` over the shared `dokploy-network`, where the generic name `api` resolved to more than one container (a stale one from a previous deploy, or another project's service). nginx pinned those IPs at startup and round-robined across them, so requests hit a refused upstream and could reach a foreign container. - api now lives only on a stack-private `internal` network, aliased as `superlist-api`; only web joins `dokploy-network` for Traefik. - nginx proxies via a variable with Docker's resolver (127.0.0.11, valid=10s), so API redeploys never leave it on a stale IP. Co-Authored-By: Claude Opus 5.5 --- AGENTS.md | 7 +++++-- README.md | 6 ++++-- apps/web/nginx.conf | 16 ++++++++++++---- docker-compose.yml | 13 ++++++++++--- 4 files changed, 31 insertions(+), 11 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 88f7487..7c5f79a 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -90,10 +90,13 @@ Run from the repo root. Verification order is `build`, then `lint` → `typechec rename never changes the TypeScript API. Default constraint/index names follow the mapped names, so do not set `map:` unless you have a reason to. - **Deployment: Docker Compose on Dokploy** (`docker-compose.yml` at the repo root). Two services, `api` and - `web`; the database is external (Dokploy's), and both join the external `dokploy-network` for Traefik. The + `web`; the database is external (Dokploy's). Only `web` joins the external `dokploy-network` for Traefik; + `web` and `api` share the stack-private `internal` network, where the API has the alias `superlist-api` + (never rely on a generic service name on the shared network: other projects can answer to it). The `api` image (`apps/api/Dockerfile`) builds the Bun bundle and runs `prisma migrate deploy` before `bun run start`; the `web` image (`apps/web/Dockerfile`) builds with Vite and serves `dist/` with - `apps/web/nginx.conf`, which also proxies `/api/*` to `http://api:4100`. Required env vars are interpolated + `apps/web/nginx.conf`, which also proxies `/api/*` to `http://superlist-api:4100` (re-resolved through + Docker's DNS, so API redeploys don't leave nginx on a stale IP). Required env vars are interpolated by Compose (`DATABASE_URL`, `BETTER_AUTH_SECRET`, `BETTER_AUTH_URL`, `CORS_ORIGIN`, `WEB_APP_URL`); set them in Dokploy. There is no hosted CI. - Generated files are gitignored and must never be edited by hand: `apps/web/src/routeTree.gen.ts`, diff --git a/README.md b/README.md index 16ead49..5574566 100644 --- a/README.md +++ b/README.md @@ -53,9 +53,11 @@ same Biome check as `biome ci` would. `docker-compose.yml` at the repo root defines the two runtime services — `api` (Bun + nginx-free Hono server) and `web` (static build behind nginx, proxying `/api/*` to the API). The database is external (provided by -Dokploy), and both services join the external `dokploy-network` so Traefik can reach them. +Dokploy). Only `web` joins the external `dokploy-network` so Traefik can reach it; `web` and `api` talk over +the stack-private `internal` network, where the API answers to the unique alias `superlist-api` (a generic +name like `api` on the shared network could resolve to another project's container). -- One public domain: nginx serves the SPA and proxies `/api/*` → `http://api:4100`. All API routes are +- One public domain: nginx serves the SPA and proxies `/api/*` → `http://superlist-api:4100`. All API routes are mounted under `/api` for that reason — never mount a route at the root of the API. - Env vars are interpolated from the Dokploy service env (`docker-compose.yml` marks `DATABASE_URL`, `BETTER_AUTH_SECRET`, `BETTER_AUTH_URL`, `CORS_ORIGIN` and `WEB_APP_URL` as required). See `.env.example`. diff --git a/apps/web/nginx.conf b/apps/web/nginx.conf index adb449c..509740a 100644 --- a/apps/web/nginx.conf +++ b/apps/web/nginx.conf @@ -9,11 +9,19 @@ server { gzip on; gzip_types text/css application/javascript application/json image/svg+xml; - # API proxy: no path rewrite, /api/* reaches the API as-is. `proxy_buffering - # off` and the long read timeout keep the SSE streams (/lists/:id/events) - # flowing in realtime instead of being batched or cut off. + # Docker's embedded DNS. Proxying through a variable makes nginx re-resolve + # the API on every TTL instead of pinning the IPs it saw at startup, so a + # redeployed API container (new IP) is picked up without restarting nginx. + resolver 127.0.0.11 valid=10s ipv6=off; + + # API proxy: no path rewrite, /api/* reaches the API as-is (proxy_pass with a + # variable and no URI forwards the original request URI). `superlist-api` is + # the API's alias on the stack-private network (see docker-compose.yml). + # `proxy_buffering off` and the long read timeout keep the SSE streams + # (/lists/:id/events) flowing in realtime instead of being batched or cut off. location /api/ { - proxy_pass http://api:4100; + set $api_upstream http://superlist-api:4100; + proxy_pass $api_upstream; proxy_http_version 1.1; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; diff --git a/docker-compose.yml b/docker-compose.yml index 3edf9c7..e882c0f 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,6 +1,9 @@ # Dokploy deployment: the API and the static frontend behind nginx. -# The database is external (Dokploy's), and the containers join the shared -# `dokploy-network` so Dokploy's Traefik can reach them via the Domains UI. +# The database is external (Dokploy's). Only `web` joins the shared +# `dokploy-network` so Dokploy's Traefik can reach it via the Domains UI; the +# API talks to nginx over the stack-private `internal` network, under the +# unique alias `superlist-api`, so no other project on `dokploy-network` +# answering to `api` can ever receive our traffic. services: api: build: @@ -23,7 +26,9 @@ services: expose: - "4100" networks: - - dokploy-network + internal: + aliases: + - superlist-api healthcheck: test: [ @@ -52,6 +57,7 @@ services: expose: - "80" networks: + - internal - dokploy-network healthcheck: test: ["CMD", "wget", "-qO", "/dev/null", "http://127.0.0.1/"] @@ -61,5 +67,6 @@ services: start_period: 10s networks: + internal: dokploy-network: external: true