A repository is recognized as Next.js if it contains next.config.js, next.config.mjs or next.config.ts, or if next appears in the dependencies of package.json. The build log shows it at step 4. Detection.
What the build runs
| Step | Command | Note |
|---|---|---|
| Install | npm ci --include=dev, or the frozen install of bun, pnpm or yarn | Depends on the committed lock file |
| Build | npm run build | Your build script, usually next build |
| Start | node server.js | Standalone output, see below |
The image uses Node.js 22, unless specified otherwise in .nvmrc, .node-version or engines.node (Supported frameworks). Next.js 16 requires Node.js 20 at minimum: an .nvmrc left on 18 makes the build fail.
The lock file must be committed and up to date with package.json, otherwise the install is refused (How builds work).
Standalone output
Next.js can produce a standalone output (standalone): a folder that contains only the server and the modules actually used. Webgune uses it by default. The runtime image then receives only .next/standalone, .next/static and public, and starts with node server.js.
Three cases, depending on your repository:
| Your repository | What Webgune does |
|---|---|
output: "standalone" in next.config | Standalone output |
No start script, or a start script equal to next start | Standalone output requested at build, without modifying your repository |
A custom start script (node server.js, next start -p 4000) | Your script is respected: the full application is kept and starts with npm start |
Declaring the mode in the repository remains the most readable:
// next.config.ts
import type { NextConfig } from "next";
const nextConfig: NextConfig = {
output: "standalone",
};
export default nextConfig;
If your version of Next.js does not produce .next/standalone/server.js, the full application is kept in the image and starts with its start script. The build log flags it.
Custom start command
You have no command to enter. If you set next start or npm start in the service's start command, remove it: the standalone image does not contain the next binary, and the service stops with a not found error (code 127). To force the command by hand, use node server.js.
Custom server
A server you write yourself (server.js with Express, for example) is declared in the start script:
{
"scripts": {
"build": "next build",
"start": "node server.js"
}
}
It must listen on process.env.PORT and on 0.0.0.0.
Environment variables
# Read on the server side only
DATABASE_URL=postgresql://user:password@host:5432/database
API_SECRET=secret-value
# Sent to the browser: prefix required
NEXT_PUBLIC_API_URL=https://api.example.com
- A variable without the
NEXT_PUBLIC_prefix is never sent to the browser. - A
NEXT_PUBLIC_*variable is written into the JavaScript code at build time. Changing it requires a new build. A restart is not enough. - By default, the build receives the service's variables. If you fill in the Build scope in the Variables panel, the build receives only those: your
NEXT_PUBLIC_*variables must then be in it (Environment variables).
Variables are not available during dependency installation, only during the build step. A postinstall script in your package.json (for example prisma generate) is therefore run after installation, with the variables.
No database during the build
The build has no access to any service in the project. A prerendered page that queries the database (generateStaticParams, a query in a static server component) makes next build fail with a connection error. Two solutions.
Render the page on demand:
// app/products/page.tsx
export const dynamic = "force-dynamic";
Or fetch the data from an API reachable over the Internet.
prisma generate does not connect to the database: it only needs DATABASE_URL to be defined. Migrations (prisma migrate deploy) cannot run during the build. See Database migrations.
Build duration
The .next/cache folder is kept from one build to the next for each service: compilation and the TypeScript check are incremental. This cache is disabled if your build, prebuild or clean script deletes the .next folder (for example rimraf .next && next build). Remove that deletion: each build already starts from a clean copy of the repository.
The Rebuild without cache button ignores the cache for one build (Deployments and rollback).
API routes, Server Actions and middleware
API routes (app/api/*), Server Actions and middleware run in the same service as the pages. No separate service is needed.
The visitor's address arrives in the X-Forwarded-For header:
// app/api/ip/route.ts
import { headers } from "next/headers";
export async function GET() {
const h = await headers();
const ip = h.get("x-forwarded-for")?.split(",")[0].trim() ?? "unknown";
return Response.json({ ip });
}
Images
next/image optimizes images on demand and writes the result to .next/cache/images, on the service's disk. This folder is wiped on every restart: images are simply recomputed on first request.
Health route
// app/healthz/route.ts
export const dynamic = "force-dynamic";
export async function GET() {
return new Response("ok", { status: 200 });
}
Health check describes what this route should verify.
Several instances
In ultra high availability, several instances serve the site. The Next.js cache (ISR, data cache) is specific to each instance and wiped on every update. Two visitors may therefore see a page regenerated at different times, and a call to revalidatePath only reaches the instance that receives it. For a shared cache, configure a cache handler (cacheHandler) that writes to Redis.
Static export
With output: "export", Next.js produces no server but an out folder. Create the service with the Static site nature: the build runs npm run build and publishes out behind Nginx (Supported frameworks).
Common errors
| Symptom | Cause | What to do |
|---|---|---|
not found, code 127 at startup | next start start command set by hand on a standalone image | Clear the service's start command |
| CSS and JavaScript returning 404 | Standalone server launched by hand from the wrong folder | Leave the default command |
next build fails on a database connection | Prerendered page that queries the database | export const dynamic = "force-dynamic" |
A NEXT_PUBLIC_* variable keeps its old value | Variable frozen at build | Redeploy |
| The install rejects the lock file | File out of date with package.json | Run npm install locally and commit |
| Stop with code 137 | Service memory exceeded | See Limits and resources |
Updated on