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:
+8
-11
@@ -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"]
|
||||
|
||||
Reference in New Issue
Block a user