More Next.js answers

by Danny
Published 3 min read

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.


_10
npm ci
_10
npm run build # next build
_10
npm 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
_10
import type { NextConfig } from 'next';
_10
_10
const nextConfig: NextConfig = {
_10
output: 'standalone',
_10
};
_10
_10
export 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
_24
FROM node:24-alpine AS deps
_24
WORKDIR /app
_24
COPY package.json package-lock.json ./
_24
RUN npm ci
_24
_24
FROM node:24-alpine AS builder
_24
WORKDIR /app
_24
COPY --from=deps /app/node_modules ./node_modules
_24
COPY . .
_24
RUN npm run build
_24
_24
FROM node:24-alpine AS runner
_24
WORKDIR /app
_24
ENV NODE_ENV=production
_24
ENV PORT=3000
_24
ENV HOSTNAME=0.0.0.0
_24
RUN addgroup -S nodejs && adduser -S nextjs -G nodejs
_24
COPY --from=builder /app/public ./public
_24
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
_24
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
_24
USER nextjs
_24
EXPOSE 3000
_24
CMD ["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
_10
const 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 (and cacheHandlers for '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_KEY at build time for every instance, or you will see "Failed to find Server Action" errors.
  • Version skew. Set deploymentId in next.config during 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 generateBuildId to 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/static and public into 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 sharp library behind next/image may 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 outputFileTracingRoot to the repo root so shared packages are traced into the standalone output.
#

Was this resource helpful?