From 318efe45c6a0207b39752cf6e501abffd64f927f Mon Sep 17 00:00:00 2001 From: Jan Wagner Date: Mon, 31 Aug 2026 20:11:22 +0200 Subject: [PATCH] Optimize: switch to output:'standalone' + slim runner image Vorher: 1.22 GB Image (volle node_modules im Container). Nachher: ~150 MB (Next.js erzeugt unter .next/standalone einen selbst-enthaltenden Server-Tree mit nur den Runtime-Dependencies). Dockerfile kopiert standalone-Output + .next/static + public, entfernt node_modules/package.json/src-Copy, startet mit 'node server.js'. --- Dockerfile | 19 ++++++++----------- next.config.ts | 4 ++++ 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/Dockerfile b/Dockerfile index 923e61d..a12564d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -18,20 +18,17 @@ RUN npm run build FROM node:22.13-bookworm-slim AS runner WORKDIR /app -ENV NODE_ENV=production NEXT_TELEMETRY_DISABLED=1 PORT=3000 +ENV NODE_ENV=production NEXT_TELEMETRY_DISABLED=1 PORT=3000 HOSTNAME=0.0.0.0 # Non-Root RUN groupadd --system --gid 1001 nodejs \ && useradd --system --uid 1001 --gid nodejs eldov -# Standalone-Output kopieren (Next.js legt ihn unter .next/standalone an, -# wenn output: 'standalone' gesetzt ist; hier vorerst klassischer next start). -COPY --from=build /app/.next ./.next -COPY --from=build /app/public ./public -COPY --from=build /app/src ./src -COPY --from=build /app/package.json ./package.json -COPY --from=build /app/next.config.ts ./next.config.ts -COPY --from=build /app/proxy.ts ./proxy.ts -COPY --from=build /app/node_modules ./node_modules +# Standalone-Output (Next.js legt ihn unter .next/standalone an). Wir kopieren +# den selbst-enthaltenden Server-Tree und die zwei statischen Ordner, die +# Next nicht mit reinpackt (public + .next/static). +COPY --from=build --chown=eldov:nodejs /app/.next/standalone ./ +COPY --from=build --chown=eldov:nodejs /app/.next/static ./.next/static +COPY --from=build --chown=eldov:nodejs /app/public ./public RUN mkdir -p /app/data && chown -R eldov:nodejs /app USER eldov EXPOSE 3000 -CMD ["npm", "run", "start"] +CMD ["node", "server.js"] diff --git a/next.config.ts b/next.config.ts index eddcd89..914e268 100644 --- a/next.config.ts +++ b/next.config.ts @@ -44,6 +44,10 @@ const config: NextConfig = { projectItemRewrite("/en/projects", "en"), ]; }, + // Standalone-Build: Next.js erzeugt unter .next/standalone einen getrimmten + // Server-Tree mit nur den Dependencies, die er zur Runtime braucht. Image + // schrumpft von ~1.2 GB auf ~150 MB. + output: "standalone", // Traefik vertraut uns — wir akzeptieren X-Forwarded-For. // (trustHostHeader ist die Next.js-Default ab 16, wenn hinter einem // Reverse-Proxy deployed.)