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'.
This commit is contained in:
Jan Wagner
2026-08-31 20:11:22 +02:00
parent f717144922
commit 318efe45c6
2 changed files with 12 additions and 11 deletions
+8 -11
View File
@@ -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"]
+4
View File
@@ -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.)