STEP 01Separate development from production
This recipe assumes a current Next.js app, an npm lockfile and Node.js 24 LTS support in your project. npm run dev is for development. First run the project checks and production build locally or in CI. Some routes may need environment variables or database access at build time.
npm ci
npm run buildSTEP 02Enable standalone output
Merge output: standalone into your existing Next.js configuration. Do not replace other project options. After building, Next creates a minimal server in .next/standalone. Public assets and .next/static need to be copied into that standalone directory.
// next.config.ts — merge into existing config
const nextConfig = { output: 'standalone' };
export default nextConfig;STEP 03Package the runtime
Use a multistage Docker build so the final image contains the built application. If your project has no public folder, create it or omit that COPY line. Add a .dockerignore that excludes .env files, node_modules, .git and .next.
FROM node:24-bookworm-slim AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
FROM node:24-bookworm-slim AS runtime
WORKDIR /app
ENV NODE_ENV=production
ENV HOSTNAME=0.0.0.0
ENV PORT=3000
COPY --from=build --chown=node:node /app/.next/standalone ./
COPY --from=build --chown=node:node /app/.next/static ./.next/static
COPY --from=build --chown=node:node /app/public ./public
USER node
EXPOSE 3000
CMD ["node", "server.js"]STEP 04Route traffic and handle state
Use the Docker Compose guide to put Caddy in front of the container. Runtime secrets belong in the runtime environment, not the Dockerfile. NEXT_PUBLIC variables are embedded at build time and are public. Uploads must persist outside the container; multi-instance caching and revalidation need additional planning.
STEP 05Test what users actually do
Check server-rendered HTML, assets, forms, authentication callbacks and image optimization. Check the logs and restart the container. Run a sample data write and verify persistence. Keep the previous image available for rollback.
COPY → YOUR AI
Take the next step to your AI.
A safe starting prompt for this guide. No secrets. Works with ChatGPT, Claude and other assistants.
Sources and technical documentation
Next.js self-hosting ↗Node.js releases ↗This recipe is a pattern for the stated prerequisites. Verify project compatibility and your actual server configuration before applying it to a live service.