examples/with-docker/README.md
A production-ready example demonstrating how to Dockerize Next.js applications using standalone mode. This example showcases best practices for containerizing Next.js apps with Docker.
The compose.yml includes both Node.js and Bun configurations. Run one service at a time to avoid port conflicts.
Node.js:
# Run with Node.js
docker compose up nextjs-standalone --build
Bun:
# OR run with Bun
docker compose up nextjs-standalone-with-bun --build
Stop the application:
docker compose down
Node.js:
# Build the image
docker build -t nextjs-standalone-image .
# Run the container
docker run -p 3000:3000 nextjs-standalone-image
Bun:
# Build the image
docker build -f Dockerfile.bun -t nextjs-standalone-bun-image .
# Run the container
docker run -p 3000:3000 nextjs-standalone-bun-image
Open your browser: Navigate to http://localhost:3000
To add Docker support to your existing Next.js project:
Dockerfile (or Dockerfile.bun for Bun) to your project root..dockerignore to your project root.next.config.js (or next.config.ts):// next.config.js
module.exports = {
output: "standalone",
};
This will build the project as a standalone app inside the Docker image.
nextjs-docker/
├── app/ # Next.js App Router directory
│ ├── layout.tsx # Root layout with metadata
│ ├── page.tsx # Home page with example content
│ └── globals.css # Global styles with Tailwind CSS v4
├── public/ # Static assets
│ └── next.svg # Next.js logo
├── Dockerfile # Multi-stage Docker configuration (Node.js)
├── Dockerfile.bun # Multi-stage Docker configuration (Bun)
├── compose.yml # Docker Compose configuration (Node.js & Bun services)
├── next.config.ts # Next.js configuration (standalone mode)
├── postcss.config.js # PostCSS configuration for Tailwind CSS
├── tsconfig.json # TypeScript configuration
├── package.json # Dependencies and scripts
└── README.md # This file
The next.config.ts file is configured with output: "standalone":
import type { NextConfig } from "next";
const nextConfig: NextConfig = {
output: "standalone",
};
export default nextConfig;
The standalone output mode creates a minimal, self-contained production build optimized for containerized deployments. When enabled, Next.js generates a .next/standalone directory containing only the essential files needed to run your application, excluding unnecessary dependencies and files. This results in significantly smaller Docker images and faster container startup times.
Learn more about Next.js standalone output in the official documentation.
dependencies), build (builder), and runtime (runner) stagesslim image tag for optimal compatibility and smaller image size/root/.npm, /usr/local/share/.cache/yarn, /root/.local/share/pnpm/store). See the Dockerfile for an optional .next/cache mount to speed up rebuilds.node user for security.next/standalone and .next/static.next directory: The .next directory is created and owned by the node user so the server can write prerender cache and optimized images at runtimeNODE_VERSION ARG to the latest LTS version for security updates.oven/bun:1 for optimal Bun performancebun user for securitybun.lock for reproducible builds.next directory for runtime cacheWhy Node.js slim image tag?: The slim variant provides optimal compatibility with npm packages and native dependencies while maintaining a smaller image size (~226MB). Slim uses glibc (standard Linux), ensuring better compatibility than Alpine's musl libc, which can cause issues with some npm packages. This makes it ideal for public examples where reliability and compatibility are priorities.
When to use Alpine?: Consider using node:24.11.1-alpine instead if:
To switch to Alpine, simply change the NODE_VERSION ARG in the Dockerfile to 24.11.1-alpine.
[!IMPORTANT] Node.js Version Maintenance: This Dockerfile uses Node.js 24.13.0-slim, which was the latest LTS version at the time of writing. To ensure security and stay up-to-date, regularly check and update the
NODE_VERSIONARG in the Dockerfile to the latest Node.js LTS version. Check the latest version at Nodejs official website and browse available Node.js images on Docker Hub.
This example can be deployed to any container-based platform:
Install the Google Cloud SDK so you can use gcloud on the command line.
Run gcloud auth login to log in to your account.
Create a new project in Google Cloud Run (e.g. nextjs-docker). Ensure billing is turned on.
Build your container image using Cloud Build:
gcloud builds submit --tag gcr.io/PROJECT-ID/nextjs-docker --project PROJECT-ID
This will also enable Cloud Build for your project.
Deploy to Cloud Run:
gcloud run deploy --image gcr.io/PROJECT-ID/nextjs-docker --project PROJECT-ID --platform managed --allow-unauthenticated
nextjs-docker.us-central1.