fix(deploy): isolate web→api traffic on a private network
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 <noreply@anthropic.com>
This commit is contained in:
@@ -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
|
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.
|
set `map:` unless you have a reason to.
|
||||||
- **Deployment: Docker Compose on Dokploy** (`docker-compose.yml` at the repo root). Two services, `api` and
|
- **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
|
`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
|
`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
|
by Compose (`DATABASE_URL`, `BETTER_AUTH_SECRET`, `BETTER_AUTH_URL`, `CORS_ORIGIN`, `WEB_APP_URL`); set them
|
||||||
in Dokploy. There is no hosted CI.
|
in Dokploy. There is no hosted CI.
|
||||||
- Generated files are gitignored and must never be edited by hand: `apps/web/src/routeTree.gen.ts`,
|
- Generated files are gitignored and must never be edited by hand: `apps/web/src/routeTree.gen.ts`,
|
||||||
|
|||||||
@@ -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)
|
`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
|
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.
|
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`,
|
- 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`.
|
`BETTER_AUTH_SECRET`, `BETTER_AUTH_URL`, `CORS_ORIGIN` and `WEB_APP_URL` as required). See `.env.example`.
|
||||||
|
|||||||
@@ -9,11 +9,19 @@ server {
|
|||||||
gzip on;
|
gzip on;
|
||||||
gzip_types text/css application/javascript application/json image/svg+xml;
|
gzip_types text/css application/javascript application/json image/svg+xml;
|
||||||
|
|
||||||
# API proxy: no path rewrite, /api/* reaches the API as-is. `proxy_buffering
|
# Docker's embedded DNS. Proxying through a variable makes nginx re-resolve
|
||||||
# off` and the long read timeout keep the SSE streams (/lists/:id/events)
|
# the API on every TTL instead of pinning the IPs it saw at startup, so a
|
||||||
# flowing in realtime instead of being batched or cut off.
|
# 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/ {
|
location /api/ {
|
||||||
proxy_pass http://api:4100;
|
set $api_upstream http://superlist-api:4100;
|
||||||
|
proxy_pass $api_upstream;
|
||||||
proxy_http_version 1.1;
|
proxy_http_version 1.1;
|
||||||
proxy_set_header Host $host;
|
proxy_set_header Host $host;
|
||||||
proxy_set_header X-Real-IP $remote_addr;
|
proxy_set_header X-Real-IP $remote_addr;
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
# Dokploy deployment: the API and the static frontend behind nginx.
|
# Dokploy deployment: the API and the static frontend behind nginx.
|
||||||
# The database is external (Dokploy's), and the containers join the shared
|
# The database is external (Dokploy's). Only `web` joins the shared
|
||||||
# `dokploy-network` so Dokploy's Traefik can reach them via the Domains UI.
|
# `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:
|
services:
|
||||||
api:
|
api:
|
||||||
build:
|
build:
|
||||||
@@ -23,7 +26,9 @@ services:
|
|||||||
expose:
|
expose:
|
||||||
- "4100"
|
- "4100"
|
||||||
networks:
|
networks:
|
||||||
- dokploy-network
|
internal:
|
||||||
|
aliases:
|
||||||
|
- superlist-api
|
||||||
healthcheck:
|
healthcheck:
|
||||||
test:
|
test:
|
||||||
[
|
[
|
||||||
@@ -52,6 +57,7 @@ services:
|
|||||||
expose:
|
expose:
|
||||||
- "80"
|
- "80"
|
||||||
networks:
|
networks:
|
||||||
|
- internal
|
||||||
- dokploy-network
|
- dokploy-network
|
||||||
healthcheck:
|
healthcheck:
|
||||||
test: ["CMD", "wget", "-qO", "/dev/null", "http://127.0.0.1/"]
|
test: ["CMD", "wget", "-qO", "/dev/null", "http://127.0.0.1/"]
|
||||||
@@ -61,5 +67,6 @@ services:
|
|||||||
start_period: 10s
|
start_period: 10s
|
||||||
|
|
||||||
networks:
|
networks:
|
||||||
|
internal:
|
||||||
dokploy-network:
|
dokploy-network:
|
||||||
external: true
|
external: true
|
||||||
|
|||||||
Reference in New Issue
Block a user