Dockerize a Next.js App (Production Multi-Stage)
Create an optimized, secure multi-stage Dockerfile for Next.js App Router using standalone output with image size under 120MB.
Goal
Build a minimal (<120MB), secure, unprivileged Docker container image for a Next.js App Router project utilizing multi-stage build caching and the standalone output mode.
Prerequisites
- Next.js 14, 15, or 16 project
- Docker Engine installed locally
```typescript
// next.config.ts
import type { NextConfig } from "next";
const nextConfig: NextConfig = {
output: "standalone",
};
export default nextConfig;
``````text
Dockerfile
.dockerignore
node_modules
.next
.git
.env*.local
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
``````dockerfile
# Stage 1: Base image
FROM node:22-alpine AS base
RUN apk add --no-cache libc6-compat
WORKDIR /app
# Stage 2: Install dependencies
FROM base AS deps
COPY package.json pnpm-lock.yaml ./
RUN corepack enable pnpm && pnpm install --frozen-lockfile
# Stage 3: Build application
FROM base AS builder
COPY --from=deps /app/node_modules ./node_modules
COPY . .
RUN corepack enable pnpm && pnpm build
# Stage 4: Production runner (minimal size, non-root user)
FROM base AS runner
ENV NODE_ENV=production
ENV PORT=3000
ENV HOSTNAME="0.0.0.0"
# Create unprivileged system user
RUN addgroup --system --gid 1001 nodejs && \
adduser --system --uid 1001 nextjs
# Copy static assets and standalone server output
COPY --from=builder /app/public ./public
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
USER nextjs
EXPOSE 3000
CMD ["node", "server.js"]
``````bash
# Build image
docker build -t my-next-app:latest .
# Run container
docker run -d --name nextjs-prod -p 3000:3000 my-next-app:latest
```Verification
Check the final image size and container logs:
# Check image size (should be ~100MB - 130MB)
docker images my-next-app:latest
# Check container health
docker logs -f nextjs-prodOpen http://localhost:3000 in your browser.
Production Security Notes
- Non-Root User: The container runs under unprivileged user
nextjs(UID 1001), preventing container escape vulnerabilities. - Minimal Surface: Alpine base image removes unnecessary system binaries, keeping the attack surface minimal.
Dockerize a Node.js Application
Step-by-step recipe to dockerize a production Node.js Express/Fastify application with multi-stage builds, non-root user, and dumb-init signal handling.
PostgreSQL with Docker Compose
Step-by-step recipe to set up a production-ready PostgreSQL instance with persistent volumes, custom initialization scripts, health checks, and pgAdmin.