How to deploy Next.js on a single VM with Docker
To deploy Next.js on a single VM with Docker, set output: "standalone" in your Next.js config, build a multi-stage image that copies only the standalone server and static files, run it with Docker Compose, and put a reverse proxy such as nginx in front. Next.js documents Docker as a deployment option that supports all features, so you give up nothing by choosing a plain VM.
This guide covers the Dockerfile, the Compose file, the proxy and a deploy routine. Every claim about Next.js behaviour comes from its docs and Docker's own Next.js guide, both read on 11 November 2026.
What you need first
A Linux VM with Docker and the Compose plugin installed, a domain pointing at the VM, and a Next.js app that builds locally with npm run build. If you are still deciding whether a VM is the right home, the Next.js deployment page lists the options: a Node.js server, a Docker container, a static export, or an adapter for a hosting platform. Node.js and Docker deployments both support all features. A static export supports a limited set.
Step 1: turn on standalone output
In next.config.ts, add:
import type { NextConfig } from "next";
const nextConfig: NextConfig = {
output: "standalone",
};
export default nextConfig;
Standalone output produces a minimal server in .next/standalone with only the runtime files it needs. The Next.js team ships a Docker standalone example built on this setting, described as a way to generate a minimal, production-ready image.
Step 2: write a multi-stage Dockerfile
Docker's Next.js guide uses three stages: one installs dependencies, one builds, and one runs. The runtime stage copies only public, .next/standalone and .next/static. Here is a version of that layout:
FROM node:24-slim AS deps
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
FROM node:24-slim AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
ENV NODE_ENV=production
RUN npm run build
FROM node:24-slim AS runner
WORKDIR /app
ENV NODE_ENV=production
ENV PORT=3000
ENV HOSTNAME="0.0.0.0"
COPY --from=builder /app/public ./public
COPY --from=builder --chown=node:node /app/.next/standalone ./
COPY --from=builder --chown=node:node /app/.next/static ./.next/static
USER node
EXPOSE 3000
CMD ["node", "server.js"]
The guide sets PORT, binds HOSTNAME to 0.0.0.0, runs as the non-root node user and starts server.js. Its own example pins an exact Node tag and tells you to check Docker Hub for a current, secure one before building, so pin a specific version in your own file rather than a floating tag.
Add a .dockerignore that excludes node_modules, .next, .git and your env files. Without it, your local build output and secrets get copied into the build context.
Step 3: handle environment variables
By default, environment variables are only available on the server. A variable prefixed with NEXT_PUBLIC_ is inlined into the JavaScript bundle during next build, so it must exist at build time. The self-hosting guide adds that server code can read variables at runtime during dynamic rendering, which lets one image move through several environments with different values.
A simple rule: put anything the browser needs in NEXT_PUBLIC_ variables and pass them as build arguments. Put secrets in runtime variables and keep them out of the image.
Step 4: run it with Compose
Create compose.yaml on the VM:
services:
web:
build: .
restart: unless-stopped
ports:
- "127.0.0.1:3000:3000"
env_file:
- .env.production
stop_grace_period: 30s
Binding to 127.0.0.1 keeps the container off the public internet. Only the reverse proxy will reach it. The 30-second grace period matters because the self-hosting guide says the server finishes in-flight requests and runs pending after() callbacks when it receives SIGINT or SIGTERM, and it recommends a drain period of 10 to 30 seconds.
Step 5: put a reverse proxy in front
The self-hosting guide recommends a reverse proxy such as nginx in front of the Next.js server rather than exposing it directly. The proxy can handle malformed requests, slow connection attacks, payload size limits and rate limiting, so the Node process spends its time rendering.
A minimal nginx server block:
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
Add TLS with your usual tool, for example Certbot. If you use streaming and Suspense, the guide says to disable proxy buffering. It shows setting an X-Accel-Buffering: no response header from next.config.
Step 6: deploy and redeploy
On the VM, clone your repository and run:
docker compose up -d --build
For later releases the routine is the same two lines:
git pull
docker compose up -d --build
Compose rebuilds the image and replaces the container. Expect a short gap while the new container starts. A single VM gives up rolling deploys. If a few seconds of downtime on release is acceptable at your stage, this setup is enough.
Step 7: check the caching and image behaviour
Two details from the docs are worth a look before you call it done.
Image optimisation through next/image works self-hosted with no extra configuration when you run the server. On glibc-based Linux, the guide notes that it may need extra configuration to prevent high memory use. If you are on a small VM, watch memory after your first traffic spike.
Page caching and Incremental Static Regeneration share one server cache, stored by default on the local disk of each instance. That works for one container. The guide says that if you later run several instances, you need a shared cache handler, a shared encryption key for Server Functions and a deployment ID. A single VM sidesteps all of that, which is a good reason to stay on one until traffic forces the issue.
When this setup stops being enough
Move off a single VM when you need zero-downtime releases, when one machine cannot handle peak traffic, or when you want the platform to manage scaling. Until then, one VM, one container and one proxy is easy to reason about and cheap to run.
If you are weighing the framework as well as the host, Next.js vs Astro for a SaaS marketing site covers when a static site is the simpler choice. For the monitoring side of a new deploy, see free uptime monitoring tools for indie SaaS.
Frequently asked questions
Do I need output standalone to run Next.js in Docker?
No, you can run next start from a full image, but standalone output copies only the files the server needs, which gives a smaller image. Docker's Next.js guide and the official Next.js example both use it.
Can one VM handle production traffic for a SaaS?
For an early product, usually yes, if you monitor memory and CPU and accept a brief gap during releases. Plan to move when you need rolling deploys or more capacity than one machine gives you.
Where should secrets live?
Keep secrets in runtime environment variables loaded through an env file or your secret manager, not in the image. Anything prefixed with NEXT_PUBLIC_ is inlined into the browser bundle at build time, so never put a secret there.
Is Docker required to self-host Next.js?
No. The Next.js docs say a Node.js server also supports all features: run npm run build and then npm run start. Docker adds a repeatable image and easier rollbacks.