DevOps

Next.js Dockerfile Generator

Generate a production-optimized Dockerfile for Next.js applications with standalone output mode, multi-stage builds, and Alpine-based minimal images.

Options

# Stage 1: Build base
FROM node:20-alpine AS base
WORKDIR /app
COPY package*.json ./
RUN npm ci

# Stage 2: Builder
FROM base AS builder
WORKDIR /app
COPY . .
RUN npm run build

# Stage 3: Runner
FROM node:20-alpine AS runner
WORKDIR /app
ENV NODE_ENV=production
ENV PORT 3000
RUN addgroup --system --gid 1001 nodejs && adduser --system --uid 1001 nextjs
USER nextjs

COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package*.json ./
COPY --from=builder /app/.next ./.next
COPY --from=builder /app/public ./public

EXPOSE 3000
CMD ["npm", "start"]

Next.js Dockerfile Generator: Complete Guide

Why Multi-Stage Builds Matter for Next.js

A naive Dockerfile that copies your entire project and runs npm install produces images exceeding 1GB. Multi-stage builds separate the build environment from the runtime environment. The builder stage installs devDependencies and compiles your Next.js app, while the runner stage copies only the compiled standalone output into a minimal Alpine image — typically under 150MB.

# Stage 1: Builder
FROM node:20-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build

# Stage 2: Runner
FROM node:20-alpine AS runner
WORKDIR /app
COPY --from=builder /app/.next/standalone ./
COPY --from=builder /app/.next/static ./.next/static
COPY --from=builder /app/public ./public
EXPOSE 3000
CMD ["node", "server.js"]

Security: Running Next.js Containers as Non-Root

By default, Docker containers run as root. If an attacker exploits a vulnerability in your Next.js application, they gain root access to the container. Always create a dedicated non-root user (e.g., 'nextjs') and switch to it with the USER directive before the CMD instruction.

How to Use

  1. 1

    Runtime Pre-Selected

    This page has pre-configured the generator for Next.js. Adjust port, base image, and environment variables as needed.

  2. 2

    Toggle Multi-Stage Build

    Enable multi-stage builds to separate the build environment from the runtime, drastically reducing image size.

  3. 3

    Enable Non-Root Security

    Check the non-root user option to run your container as a non-privileged user for production security.

  4. 4

    Copy & Use

    Switch between Dockerfile, docker-compose.yml, and .dockerignore tabs to copy each file.

Frequently Asked Questions

How do I Dockerize a Next.js application?+

Set output: 'standalone' in next.config.js, then use a multi-stage Dockerfile: Stage 1 installs dependencies and builds with 'npm run build'. Stage 2 copies only the .next/standalone directory into a lightweight Node.js Alpine image and runs 'node server.js'.

What is Next.js standalone output mode for Docker?+

The standalone output mode (output: 'standalone' in next.config.js) bundles only the files needed for production into a self-contained directory. This can reduce your Docker image size by 80% or more compared to copying the entire node_modules folder.

Should I use Alpine or Debian Slim for a Next.js Docker image?+

Alpine produces images around 50MB smaller, but some native Node.js packages (like sharp for image optimization) may need extra build dependencies. Debian Slim is safer for apps with native dependencies. For most Next.js apps, Alpine works perfectly.