next.js icon indicating copy to clipboard operation
next.js copied to clipboard

[feat] Add Docker examples for standalone output and export output

Open kristiyan-velkov opened this issue 1 week ago • 2 comments

What?

Adds two new production-ready Docker examples demonstrating different Next.js deployment strategies, following best practices for containerizing a Next.js application:

  1. with-docker-standalone-output - Demonstrates Next.js standalone mode deployment with Docker, featuring a comprehensive multi-stage Dockerfile following Docker best practices, BuildKit cache mounts for faster builds, Docker Compose configuration, and extensive documentation.

  2. with-docker-export-output - Demonstrates Next.js static export mode deployment with Docker, offering two serving options: Nginx (production-grade) and serve package (Node.js-based, simpler setup). Includes optimized Nginx configuration, BuildKit cache mounts, and comprehensive documentation explaining when to use each approach.

Both examples include detailed documentation explaining Docker best practices, implementation decisions, and deployment guidance.

Why?

The existing with-docker example provides basic Docker support but lacks:

  • Detailed documentation explaining Docker best practices and implementation decisions
  • BuildKit cache mounts for optimized build performance
  • Comprehensive READMEs with deployment guidance and configuration explanations
  • Clear documentation about Node.js image variant choices (slim vs Alpine)
  • Docker Compose configurations for easier local development
  • Examples demonstrating different Next.js output modes (standalone vs static export)
  • Guidance on choosing between different serving strategies (Nginx vs Node.js)

These new examples serve as comprehensive references for developers who want to understand:

  • How to Dockerize Next.js applications for different deployment scenarios
  • Why certain choices are made and how to optimize Docker setups for production
  • When to use standalone mode vs static export mode
  • How to choose between different web servers (Nginx vs serve) for static sites

How?

with-docker-standalone-output example:

  • Multi-stage Dockerfile with three stages: dependencies installation, build, and runtime
  • BuildKit cache mounts for package manager stores (npm, yarn, pnpm) and Next.js build cache
  • Security best practices with non-root user execution
  • Docker Compose configuration (compose.yml) for simplified local development
  • Comprehensive .dockerignore to minimize build context size
  • Detailed README explaining standalone mode benefits, Node.js image choices, and deployment guidance

with-docker-export-output example:

  • Two Dockerfile options:
    • Dockerfile - Nginx-based serving (~50MB final image) with optimized production configuration
    • Dockerfile.serve - Node.js serve package-based serving for simpler deployments
  • Multi-stage builds for both options with separate dependency, build, and runtime stages
  • BuildKit cache mounts for package manager stores and Next.js build cache
  • Production Nginx configuration (nginx.conf) with gzip compression, caching headers, and security best practices
  • Docker Compose configuration (compose.yml) supporting both serving options
  • Comprehensive README explaining static export mode, trade-offs between Nginx and serve, and deployment guidance

Documentation highlights (both examples):

  • Detailed explanations of Next.js output modes (standalone vs static export) and their use cases
  • Clear explanation of Node.js image variant choices (slim vs Alpine) with trade-offs
  • Step-by-step quick start instructions for both Docker and Docker Compose
  • Project structure documentation
  • Links to relevant Next.js and Docker documentation

Docker best practices implemented (both examples):

  • Multi-stage builds for optimal image size reduction
  • Layer caching optimization by copying package files first
  • Minimal build context via comprehensive .dockerignore files
  • BuildKit cache mounts for faster subsequent builds
  • Security hardening with non-root user execution
  • Explicit configuration through environment variables
  • Production-ready with proper file permissions and optimized layers
  • Package manager support for npm, yarn, and pnpm with automatic detection

Both examples follow Docker best practices and Next.js output requirements, providing developers with reliable, production-ready references that demonstrate both the technical implementation and the reasoning behind each decision.


Note: After this PR is approved and merged, I plan to open a follow-up PR to:

  • Update the Next.js documentation with proper links to these new Docker examples
  • Review and suggest removal of any outdated or redundant Docker examples that may no longer be needed, ensuring the examples directory remains clean and maintainable

kristiyan-velkov avatar Dec 11 '25 15:12 kristiyan-velkov

Allow CI Workflow Run

  • [x] approve CI run for commit: 224f27688759fa5ea2ecd9f1d91a3706aa9eb926

Note: this should only be enabled once the PR is ready to go and can only be enabled by a maintainer

nextjs-bot avatar Dec 11 '25 15:12 nextjs-bot

Allow CI Workflow Run

  • [ ] approve CI run for commit: 0d8c831c0a4ee09999c0efccc221e4e1e46f5543

Note: this should only be enabled once the PR is ready to go and can only be enabled by a maintainer

nextjs-bot avatar Dec 11 '25 15:12 nextjs-bot