Run next build and next start on any Node.js server, or use output: standalone for a small Docker image. Caching, multiple instances and streaming need a little extra setup.
The short answer
#Next.js runs on any server with Node.js. Build it, then start the built-in server. Next.js 16 needs Node.js 20.9 or later, but Node 20 is past end of life, so use Node 24 or 26.
_10npm ci_10npm run build # next build_10npm run start # next start, listens on port 3000
For containers, set output: 'standalone' in next.config.ts. The build then produces a small folder you can copy into a Docker image without installing node_modules.
Why it happens
#People often assume Next.js needs Vercel because some features feel tied to the platform. They are not. Server Components, Route Handlers, Server Actions, proxy.ts, image optimisation, ISR and Cache Components all work with next start.
What Vercel adds is infrastructure around the app: a CDN, a shared cache across many instances, and coordination between deployments. When you host it yourself, a single server with a persistent disk works out of the box. Running several instances, or putting a proxy in front, is where you need extra setup.
How to fix it
#Build a standalone Docker image
Enable standalone output:
_10// next.config.ts_10import type { NextConfig } from 'next';_10_10const nextConfig: NextConfig = {_10 output: 'standalone',_10};_10_10export default nextConfig;
next build now writes .next/standalone, which contains a minimal server.js and only the dependencies your code uses. It does not include public or .next/static, because those are meant for a CDN, so copy them in yourself. A multi-stage Dockerfile:
_24# Dockerfile_24FROM node:24-alpine AS deps_24WORKDIR /app_24COPY package.json package-lock.json ./_24RUN npm ci_24_24FROM node:24-alpine AS builder_24WORKDIR /app_24COPY --from=deps /app/node_modules ./node_modules_24COPY . ._24RUN npm run build_24_24FROM node:24-alpine AS runner_24WORKDIR /app_24ENV NODE_ENV=production_24ENV PORT=3000_24ENV HOSTNAME=0.0.0.0_24RUN addgroup -S nodejs && adduser -S nextjs -G nodejs_24COPY --from=builder /app/public ./public_24COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./_24COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static_24USER nextjs_24EXPOSE 3000_24CMD ["node", "server.js"]
Add a .dockerignore with node_modules, .next, .git and your .env* files, so secrets stay out of the image. HOSTNAME=0.0.0.0 matters: without it the server may only listen inside the container and you cannot reach it.
Put a reverse proxy in front
The Next.js docs recommend nginx, Caddy or a similar proxy rather than exposing Node directly. It handles TLS, slow clients, payload limits and rate limiting. If you use streaming, loading.tsx or Suspense, turn off response buffering, or users see nothing until the whole page is ready. For nginx, you can send X-Accel-Buffering: no from Next.js:
_10// next.config.ts_10const nextConfig: NextConfig = {_10 output: 'standalone',_10 async headers() {_10 return [{ source: '/:path*', headers: [{ key: 'X-Accel-Buffering', value: 'no' }] }];_10 },_10};
Handle environment variables
Server-only variables are read at runtime, so one image can move between staging and production. NEXT_PUBLIC_ variables are different: they are written into the browser bundle during next build. Changing them means rebuilding the image.
Extra setup for more than one instance
If you run several containers behind a load balancer:
- Shared cache. ISR pages and cached data are stored on each instance's disk and in memory. Point
cacheHandler(andcacheHandlersfor'use cache') at shared storage such as Redis, so a revalidation on one instance applies to all. - Server Actions key. Set the same
NEXT_SERVER_ACTIONS_ENCRYPTION_KEYat build time for every instance, or you will see "Failed to find Server Action" errors. - Version skew. Set
deploymentIdinnext.configduring rolling deploys, so clients on an old version do a full reload instead of requesting files that no longer exist. - Same build everywhere. Build once and ship the same image to every instance. If you must rebuild per environment, use
generateBuildIdto keep the ID consistent.
Shut down cleanly
Send SIGTERM or SIGINT and give the process 10 to 30 seconds. Next.js finishes in-flight requests and any pending after() callbacks before it exits.
Common pitfalls
#- Missing CSS and images. If the page loads unstyled, you forgot to copy
.next/staticandpublicinto the standalone folder. - Running next dev in production. Dev mode is slow and not secure. Always run a production build.
- Image optimisation memory. On glibc-based Linux, the
sharplibrary behindnext/imagemay need a different memory allocator to avoid high memory use. See the sharp install docs. - Expecting static export to cover everything.
output: 'export'produces plain HTML, but drops proxy, Server Actions, ISR and default image optimisation. - Monorepo files left out. Set
outputFileTracingRootto the repo root so shared packages are traced into the standalone output.
Related reading
#Was this resource helpful?