Originally published on the OpsCrew blog .
TL;DR
Add output: "standalone" to next.config.ts so the build emits a self-contained server.js that runs with plain Node.js.
Build a three-stage Docker image (dependencies, build, run) that copies .next/standalone , .next/static and public , and starts as the non-root node user.
Run it with Docker Compose behind Caddy, which gets the HTTPS certificate itself, and next to PostgreSQL, which publishes no port.
NEXT_PUBLIC_ variables are baked into the bundle at build time, so set them where the image is built, not on the server.
To deploy Next.js to a VPS, turn on output: "standalone" , build a small Docker image from the standalone output, and run it on the server with Docker Compose, next to PostgreSQL and behind Caddy, which gets and renews the HTTPS certificate on its own .…