Self-Hosting & Docker
next build to produce an optimized production build, then next start to serve it. That pair of commands works on any machine with Node.js installed — a bare VM, an existing Kubernetes cluster, or a container platform like AWS ECS, Fly.io, or Railway.The self-hosting build/run cycle
npm run build # runs `next build` — compiles and optimizes for production npm run start # runs `next start` — serves the build on port 3000 by default
Packaging with Docker
output: 'standalone' config option.next.config.mjs
/** @type {import('next').NextConfig} */
const nextConfig = {
output: 'standalone',
}
export default nextConfigstandalone enabled, next build emits a .next/standalone folder that contains a minimal server and only the node_modules your app actually needs at runtime — copied automatically, without a separate pruning step. That folder can be copied into a tiny final image instead of shipping your entire node_modules tree.Dockerfile — minimal multi-stage build
FROM node:20-alpine AS deps WORKDIR /app COPY package.json package-lock.json ./ RUN npm ci FROM node:20-alpine AS builder WORKDIR /app COPY --from=deps /app/node_modules ./node_modules COPY . . RUN npm run build FROM node:20-alpine AS runner WORKDIR /app ENV NODE_ENV=production 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"]
Build and run the image
docker build -t my-next-app . docker run -p 3000:3000 my-next-app
What "standalone" saves you
Without | With |
|---|---|
Final image needs the full | Only the traced, actually-used dependencies are included |
Runtime start command is | Runtime start command is |
next build+next startis all a self-hosted deployment fundamentally needs — any Node.js-capable host works.Set
output: 'standalone'innext.config.mjsso the build only includes the dependencies actually used at runtime.A multi-stage Dockerfile keeps the final image small by discarding build-time-only files and dependencies.
Self-hosting means you are responsible for scaling, monitoring, and deploy orchestration that a managed platform would otherwise provide.