From 393e8c01cd84566e041f169486ebff8a0d0734f3 Mon Sep 17 00:00:00 2001 From: denshooter Date: Fri, 9 Jan 2026 02:36:21 +0100 Subject: [PATCH] feat: Enhance Dockerfile with verification for standalone output and update n8n status route to handle missing webhook URL --- DOCKER_BUILD_FIX.md | 149 ++++++++++++++++++++++++++++++++++++ Dockerfile | 16 ++++ app/api/n8n/status/route.ts | 15 +++- 3 files changed, 179 insertions(+), 1 deletion(-) create mode 100644 DOCKER_BUILD_FIX.md diff --git a/DOCKER_BUILD_FIX.md b/DOCKER_BUILD_FIX.md new file mode 100644 index 0000000..9d9c6d6 --- /dev/null +++ b/DOCKER_BUILD_FIX.md @@ -0,0 +1,149 @@ +# Docker Build Fix - Standalone Output Issue + +## Problem + +Der Docker Build schlägt fehl mit: +``` +ERROR: failed to calculate checksum of ref ... "/app/.next/standalone/app": not found +``` + +## Ursache + +Next.js erstellt das `standalone` Output nur, wenn: +1. `output: "standalone"` in `next.config.ts` gesetzt ist ✅ (bereits konfiguriert) +2. Der Build erfolgreich abgeschlossen wird +3. Alle Abhängigkeiten korrekt aufgelöst werden + +## Lösung + +### 1. n8n Status Route Fix + +Die Route wurde angepasst, um während des Builds nicht zu fehlschlagen, wenn `N8N_WEBHOOK_URL` nicht gesetzt ist: + +```typescript +// Prüft jetzt, ob N8N_WEBHOOK_URL gesetzt ist +if (!n8nWebhookUrl) { + return NextResponse.json({ /* fallback */ }); +} +``` + +### 2. Dockerfile Verbesserungen + +- **Verification Step**: Prüft, ob das standalone Verzeichnis existiert +- **Debug Output**: Zeigt die Verzeichnisstruktur, falls Probleme auftreten +- **Robustere Fehlerbehandlung**: Bessere Fehlermeldungen + +### 3. Mögliche Ursachen und Lösungen + +#### Problem: Standalone Output wird nicht erstellt + +**Lösung 1: Prüfe next.config.ts** +```typescript +// Stelle sicher, dass dies gesetzt ist: +output: "standalone", +outputFileTracingRoot: path.join(process.cwd()), +``` + +**Lösung 2: Prüfe Build-Logs** +```bash +# Schaue in die Build-Logs, ob es Fehler gibt +docker build . 2>&1 | grep -i "standalone\|error" +``` + +**Lösung 3: Lokaler Test** +```bash +# Teste lokal, ob standalone erstellt wird +npm run build +ls -la .next/standalone/ +``` + +#### Problem: Falsche Verzeichnisstruktur + +Next.js 15 könnte eine andere Struktur haben. Prüfe: +```bash +# Nach dem Build +find .next/standalone -name "server.js" +``` + +Falls `server.js` in `.next/standalone/app/server.js` ist, ist das Dockerfile korrekt. +Falls es in `.next/standalone/server.js` ist, muss das Dockerfile angepasst werden. + +## Debugging + +### 1. Lokaler Build Test + +```bash +# Baue lokal +npm run build + +# Prüfe ob standalone existiert +test -d .next/standalone && echo "✅ Standalone exists" || echo "❌ Standalone missing" + +# Zeige Struktur +ls -la .next/standalone/ +find .next/standalone -name "server.js" +``` + +### 2. Docker Build mit Debug + +```bash +# Baue mit mehr Output +docker build --progress=plain -t portfolio-app:test . + +# Oder baue nur bis zum Builder Stage +docker build --target builder -t portfolio-builder:test . +docker run --rm portfolio-builder:test ls -la .next/standalone/ +``` + +### 3. Prüfe Build-Logs + +Der aktualisierte Dockerfile gibt jetzt Debug-Informationen aus: +- Zeigt `.next/` Verzeichnisstruktur +- Sucht nach `standalone` Verzeichnis +- Zeigt `server.js` Location + +## Alternative: Fallback ohne Standalone + +Falls das standalone Output weiterhin Probleme macht, kann man auf ein vollständiges Image zurückgreifen: + +```dockerfile +# Statt standalone zu kopieren, kopiere alles +COPY --from=builder --chown=nextjs:nodejs /app/node_modules ./node_modules +COPY --from=builder --chown=nextjs:nodejs /app/.next ./.next +COPY --from=builder --chown=nextjs:nodejs /app/public ./public +COPY --from=builder --chown=nextjs:nodejs /app/package.json ./package.json +COPY --from=builder --chown=nextjs:nodejs /app/prisma ./prisma + +CMD ["npm", "start"] +``` + +**Nachteil**: Größeres Image, aber funktioniert immer. + +## Nächste Schritte + +1. ✅ n8n Status Route Fix (bereits gemacht) +2. ✅ Dockerfile Debug-Verbesserungen (bereits gemacht) +3. 🔄 Push zum dev Branch und Build testen +4. 📊 Build-Logs analysieren +5. 🔧 Falls nötig: Dockerfile weiter anpassen + +## Workflow Test + +```bash +# 1. Committe Änderungen +git add . +git commit -m "Fix: Docker build standalone output issue" + +# 2. Push zum dev Branch +git push origin dev + +# 3. Überwache Gitea Actions +# Gehe zu Repository → Actions → CI/CD Pipeline (Dev/Staging) + +# 4. Prüfe Build-Logs +# Schaue nach den Debug-Ausgaben im Build-Step +``` + +--- + +**Hinweis**: Falls das Problem weiterhin besteht, schaue in die Build-Logs nach den Debug-Ausgaben, die der aktualisierte Dockerfile jetzt ausgibt. Diese zeigen genau, wo das Problem liegt. diff --git a/Dockerfile b/Dockerfile index 4818a25..c7762fc 100644 --- a/Dockerfile +++ b/Dockerfile @@ -35,6 +35,20 @@ ENV NEXT_TELEMETRY_DISABLED=1 ENV NODE_ENV=production RUN npm run build +# Verify standalone output was created and show structure for debugging +RUN if [ ! -d .next/standalone ]; then \ + echo "ERROR: .next/standalone directory not found!"; \ + echo "Contents of .next directory:"; \ + ls -la .next/ || true; \ + echo "Checking if standalone exists in different location:"; \ + find .next -name "standalone" -type d || true; \ + exit 1; \ + fi && \ + echo "✅ Standalone output found" && \ + ls -la .next/standalone/ && \ + echo "Standalone structure:" && \ + find .next/standalone -type f -name "server.js" || echo "server.js not found in standalone" + # Production image, copy all the files and run next FROM base AS runner WORKDIR /app @@ -55,6 +69,8 @@ RUN chown nextjs:nodejs .next # Automatically leverage output traces to reduce image size # https://nextjs.org/docs/advanced-features/output-file-tracing +# Copy standalone output (contains server.js and all dependencies) +# The standalone output structure is: .next/standalone/app/ COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone/app ./ COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static diff --git a/app/api/n8n/status/route.ts b/app/api/n8n/status/route.ts index 1eedb39..85f2a52 100644 --- a/app/api/n8n/status/route.ts +++ b/app/api/n8n/status/route.ts @@ -6,10 +6,23 @@ export const revalidate = 30; export async function GET() { try { + // Check if n8n webhook URL is configured + const n8nWebhookUrl = process.env.N8N_WEBHOOK_URL; + + if (!n8nWebhookUrl) { + // Return fallback if n8n is not configured + return NextResponse.json({ + status: { text: "offline", color: "gray" }, + music: null, + gaming: null, + coding: null, + }); + } + // Rufe den n8n Webhook auf // Add timestamp to query to bypass Cloudflare cache const res = await fetch( - `${process.env.N8N_WEBHOOK_URL}/webhook/denshooter-71242/status?t=${Date.now()}`, + `${n8nWebhookUrl}/webhook/denshooter-71242/status?t=${Date.now()}`, { method: "GET", headers: {