Next.js Deployment & Production
Deploy Next.js applications to Vercel, Docker containers, self-hosted Node.js servers, and static hosts.
Overview
Next.js applications can be deployed anywhere Node.js or static hosting is supported, including managed platforms like Vercel, Docker containers, Kubernetes, or self-hosted virtual machines.
1. Managed Deployment (Vercel)
Vercel is the primary platform built by the creators of Next.js, offering zero-configuration deployments:
- Push your code to a GitHub, GitLab, or Bitbucket repository.
- Import the project in the Vercel Dashboard.
- Configure environment variables in project settings.
- Automatic preview builds for pull requests and instant production deployments on
main.
2. Docker Container Deployment
For self-hosting on AWS ECS, Google Cloud Run, Fly.io, or VPS servers, use Next.js standalone output mode to generate a lean production container:
Step 1: Enable Standalone Output
// next.config.ts
import type { NextConfig } from 'next';
const nextConfig: NextConfig = {
output: 'standalone',
};
export default nextConfig;Step 2: Multi-Stage Dockerfile
# Multi-stage build for Next.js standalone
FROM node:22-alpine AS base
FROM base AS deps
WORKDIR /app
RUN corepack enable pnpm
COPY package.json pnpm-lock.yaml ./
RUN pnpm install --frozen-lockfile
FROM base AS builder
WORKDIR /app
RUN corepack enable pnpm
COPY --from=deps /app/node_modules ./node_modules
COPY . .
ENV NEXT_TELEMETRY_DISABLED=1
RUN pnpm build
FROM base AS runner
WORKDIR /app
ENV NODE_ENV=production
ENV PORT=3000
ENV HOSTNAME="0.0.0.0"
# Copy standalone build
COPY --from=builder /app/public ./public
COPY --from=builder /app/.next/standalone ./
COPY --from=builder /app/.next/static ./.next/static
EXPOSE 3000
CMD ["node", "server.js"]3. Static HTML Export
If your application doesn't require server features (no Route Handlers, Server Actions, or dynamic cookies), you can export pure static HTML:
// next.config.ts
import type { NextConfig } from 'next';
const nextConfig: NextConfig = {
output: 'export',
images: { unoptimized: true }, // Required unless using custom loader
};
export default nextConfig;Running pnpm build outputs static files to the out/ directory, which can be hosted on GitHub Pages, Cloudflare Pages, S3, or NGINX.
Pre-Deployment Checklist
- Run
pnpm buildlocally to catch type errors and build warnings. - Configure production environment variables (
DATABASE_URL, API keys). - Set
NEXT_TELEMETRY_DISABLED=1in CI/CD pipelines. - Confirm all cache revalidation tokens and webhooks are secured.