Next.js Deployment
Deploy a Next.js app to Vercel, a Node.js server, a Docker container, or static hosting.
TL;DR
- Build with
next build, then serve the app withnext start. - Set
output: 'standalone'to produce a small server for Docker. - Set
output: 'export'to generate a fully static site.
Build and Run
next buildCreate the optimized production build and catch type and prerender errors.
npm run buildnext startServe the production build with the built-in Node.js server.
npm run start
# or choose a port
npx next start -p 8080Build-Time VariablesProvide public variables during the build, because they are inlined into the code.
NEXT_PUBLIC_API_URL="https://api.example.com" \
npm run buildVercel
git pushConnect the repository once, then every push to main deploys automatically.
git push origin main
# Vercel builds and deploys the commitPreview DeploymentsPush a branch and open a pull request to get a private preview URL.
git checkout -b new-feature
git push origin new-feature
# the pull request gets its own URLvercelDeploy from the terminal with the Vercel command-line tool.
npm install -g vercel
vercel # preview deployment
vercel --prod # production deploymentDocker and Standalone
output: 'standalone'Produce a minimal server folder that needs no separate node_modules install.
// next.config.ts
const nextConfig = {
output: 'standalone',
};Copy Static FilesAdd the public and static folders, which the standalone output leaves out.
cp -r public .next/standalone/
cp -r .next/static .next/standalone/.next/
node .next/standalone/server.jsDockerfileBuild in one stage and copy only the standalone output into the final image.
FROM node:22-alpine AS build
WORKDIR /app
COPY . .
RUN npm ci && npm run build
FROM node:22-alpine
WORKDIR /app
COPY --from=build /app/.next/standalone ./
COPY --from=build /app/.next/static ./.next/static
COPY --from=build /app/public ./public
CMD ["node", "server.js"]HOSTNAME and PORTListen on all interfaces inside a container so the host can reach the app.
ENV HOSTNAME=0.0.0.0
ENV PORT=3000
EXPOSE 3000Static Export
output: 'export'Generate plain HTML, CSS, and JavaScript files with no server required.
const nextConfig = {
output: 'export',
};outFind the exported site in the out folder and upload it to any static host.
npm run build
npx serve outimages.unoptimizedTurn off the image optimizer, since a static export has no server to run it.
const nextConfig = {
output: 'export',
images: { unoptimized: true },
};Tips
- Run
npm run buildlocally before every deploy, because type errors and prerender failures stop the production build. - Put a reverse proxy such as nginx in front of
next start, so it handles slow connections and malformed requests.
Warnings
- The
standaloneserver does not includepublicor.next/static, so copy both folders into the output before starting it. - A static
output: 'export'build cannot use Server Functions, cookies, or the proxy file, because no server runs.
In Practice
Package a Next.js app into a small Docker image using the standalone output.
- The build stage installs dependencies and runs the production build.
- The final stage starts clean, so build tools never reach production.
- Only the standalone server, static files, and public folder are copied in.
- HOSTNAME is set to all interfaces, so the container accepts outside traffic.
# Requires output: 'standalone' in next.config.ts
FROM node:22-alpine AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
FROM node:22-alpine
WORKDIR /app
ENV NODE_ENV=production
ENV HOSTNAME=0.0.0.0
ENV PORT=3000
COPY --from=build /app/.next/standalone ./
COPY --from=build /app/.next/static ./.next/static
COPY --from=build /app/public ./public
EXPOSE 3000
CMD ["node", "server.js"]FAQ
The simplest path is to connect your git repository to Vercel, which builds and deploys on every push. You can also run next build and next start on any Node.js server, or ship a Docker image.
Yes. Next.js runs anywhere Node.js runs. Use next start on a server, or build a Docker image with output: 'standalone'. All features are supported when you self-host.
It copies only the files the server needs into .next/standalone, including the required parts of node_modules. You run it with node server.js, which keeps Docker images small.
Set output: 'export' in next.config.ts and run next build. The HTML, CSS, and JavaScript land in the out folder, ready for any static host.