1
0
Fork 0

feat: containerize the Next.js app and wire it into docker-compose

Adds web/Dockerfile (multi-stage node:22-alpine build, standalone output)
and web/.dockerignore, and wires a `web` service into docker-compose.yaml
so the whole stack (self-hosted Supabase + Next.js) starts with a single
`docker compose up -d`.

Networking fix: Next.js inlines NEXT_PUBLIC_* env vars at build time into
every compiled bundle, including server-side ones (Server Actions, Server
Components, Middleware) - not just the browser bundle. Since
NEXT_PUBLIC_SUPABASE_URL must point at the externally-reachable
http://localhost:8000 for the browser, the server-side Supabase clients in
lib/supabase/server.ts and lib/supabase/middleware.ts got that same literal
baked in - which resolves to the web container itself (not kong) once
running in Docker, causing ECONNREFUSED on any server-side Supabase call
(most visibly signInWithPassword in the login server action).

Fix: introduce SUPABASE_INTERNAL_URL, a server-only env var without the
NEXT_PUBLIC_ prefix, so it's read at actual runtime rather than inlined at
build time. It points at http://kong:8000 in docker-compose.yaml (same
pattern already used by the studio service's internal SUPABASE_URL) and at
http://localhost:8000 in web/.env.local(.example) for non-Docker local dev,
where there's no container networking boundary. lib/supabase/client.ts
(browser-only) is untouched and keeps reading NEXT_PUBLIC_SUPABASE_URL.

Considered extra_hosts: ["localhost:host-gateway"] instead, but rejected it:
overriding what the literal string "localhost" resolves to inside a
container is unreliable (glibc's resolver typically still prioritizes the
container's own loopback), and is the kind of workaround that looks fixed
in a quick check and quietly breaks later.

Verified end-to-end against the running container via curl (unauth redirect,
real signInWithPassword through the login server action, authenticated
dashboard content, logout) with zero ECONNREFUSED in `docker compose logs
web` afterward, plus a local `npm run dev` smoke test to confirm non-Docker
development still works unchanged.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Jonny 2026-07-05 00:25:01 +02:00
parent c236e61692
commit 2f508ea5dd
6 changed files with 71 additions and 2 deletions

View File

@ -149,3 +149,29 @@ services:
# Keys eingesetzt, siehe Template-Kommentar) -- Kong selbst subsituiert # Keys eingesetzt, siehe Template-Kommentar) -- Kong selbst subsituiert
# keine Env-Vars in der deklarativen Config. # keine Env-Vars in der deklarativen Config.
- ./volumes/api/kong.yml:/home/kong/kong.yml:ro - ./volumes/api/kong.yml:/home/kong/kong.yml:ro
web:
build:
context: ./web
args:
NEXT_PUBLIC_SUPABASE_URL: ${NEXT_PUBLIC_SUPABASE_URL}
NEXT_PUBLIC_SUPABASE_ANON_KEY: ${NEXT_PUBLIC_SUPABASE_ANON_KEY}
restart: unless-stopped
ports:
- "3000:3000"
depends_on:
- kong
environment:
NEXT_PUBLIC_SUPABASE_URL: ${NEXT_PUBLIC_SUPABASE_URL}
NEXT_PUBLIC_SUPABASE_ANON_KEY: ${NEXT_PUBLIC_SUPABASE_ANON_KEY}
# Next.js baeckt NEXT_PUBLIC_*-Variablen zur Build-Zeit auch in
# serverseitige Bundles ein (Server Actions/Components, Middleware).
# Server-seitiger Code im Container darf deshalb nicht die
# browser-gerichtete NEXT_PUBLIC_SUPABASE_URL (localhost:8000, von
# aussen erreichbar) verwenden -- im Container zeigt "localhost" auf
# den Container selbst statt auf kong, was zu ECONNREFUSED fuehrt.
# SUPABASE_INTERNAL_URL hat kein NEXT_PUBLIC_-Praefix, wird also erst
# zur Laufzeit gelesen (kein Build-Time-Inlining) und zeigt hier auf
# den internen Docker-Hostnamen -- gleiches Muster wie SUPABASE_URL
# beim studio-Service oben.
SUPABASE_INTERNAL_URL: http://kong:8000

4
web/.dockerignore Normal file
View File

@ -0,0 +1,4 @@
node_modules
.next
.env.local
npm-debug.log

View File

@ -1,2 +1,7 @@
NEXT_PUBLIC_SUPABASE_URL=http://localhost:8000 NEXT_PUBLIC_SUPABASE_URL=http://localhost:8000
NEXT_PUBLIC_SUPABASE_ANON_KEY= NEXT_PUBLIC_SUPABASE_ANON_KEY=
# Ohne Docker gibt es keine Netzwerkgrenze zwischen "web" und Kong, daher hier
# derselbe (von aussen erreichbare) Wert wie NEXT_PUBLIC_SUPABASE_URL. Im
# Docker-Container (docker-compose.yaml) zeigt diese Variable stattdessen auf
# den internen Hostnamen http://kong:8000.
SUPABASE_INTERNAL_URL=http://localhost:8000

23
web/Dockerfile Normal file
View File

@ -0,0 +1,23 @@
FROM node:22-alpine AS deps
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
FROM node:22-alpine AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
ARG NEXT_PUBLIC_SUPABASE_URL
ARG NEXT_PUBLIC_SUPABASE_ANON_KEY
ENV NEXT_PUBLIC_SUPABASE_URL=${NEXT_PUBLIC_SUPABASE_URL}
ENV NEXT_PUBLIC_SUPABASE_ANON_KEY=${NEXT_PUBLIC_SUPABASE_ANON_KEY}
RUN npm run build
FROM node:22-alpine AS runner
WORKDIR /app
ENV NODE_ENV=production
COPY --from=builder /app/public ./public
COPY --from=builder /app/.next/standalone ./
COPY --from=builder /app/.next/static ./.next/static
EXPOSE 3000
CMD ["node", "server.js"]

View File

@ -5,7 +5,10 @@ export async function updateSession(request: NextRequest) {
let supabaseResponse = NextResponse.next({ request }); let supabaseResponse = NextResponse.next({ request });
const supabase = createServerClient( const supabase = createServerClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!, // Serverseitig muss die interne (Docker-)URL verwendet werden, nicht die
// browser-gerichtete NEXT_PUBLIC_SUPABASE_URL — siehe Begruendung in
// lib/supabase/server.ts.
process.env.SUPABASE_INTERNAL_URL!,
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!, process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
{ {
cookies: { cookies: {

View File

@ -5,7 +5,15 @@ export async function createClient() {
const cookieStore = await cookies(); const cookieStore = await cookies();
return createServerClient( return createServerClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!, // SUPABASE_INTERNAL_URL statt NEXT_PUBLIC_SUPABASE_URL: Next.js baeckt
// NEXT_PUBLIC_*-Variablen zur Build-Zeit auch in serverseitige Bundles
// ein. Im Docker-Container muesste dieser serverseitige Code sonst mit
// der browser-gerichteten (von aussen erreichbaren) URL sprechen, was
// dort auf ECONNREFUSED laeuft (localhost zeigt im Container auf sich
// selbst statt auf den kong-Service). SUPABASE_INTERNAL_URL wird ohne
// NEXT_PUBLIC_-Praefix zur Laufzeit gelesen und kann pro Umgebung
// unterschiedlich gesetzt werden (siehe docker-compose.yaml/.env.local).
process.env.SUPABASE_INTERNAL_URL!,
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!, process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
{ {
cookies: { cookies: {