🌐 AI搜索 & 代理 主页
Skip to content

Conversation

@kristiyan-velkov
Copy link

@kristiyan-velkov kristiyan-velkov commented Dec 11, 2025

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

@nextjs-bot nextjs-bot added the examples Issue was opened via the examples template. label Dec 11, 2025
@nextjs-bot
Copy link
Collaborator

Allow CI Workflow Run

  • approve CI run for commit: 224f276

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

@nextjs-bot
Copy link
Collaborator

nextjs-bot commented Dec 11, 2025

Allow CI Workflow Run

  • approve CI run for commit: 5883187

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

kristiyan-velkov and others added 4 commits December 11, 2025 17:10
- Fix PORT environment variable to be explicitly a string in compose.yml
- Remove NextConfig type import from next.config.ts to fix TypeScript error
…stiyan-velkov/next.js into examples/add-docker-examples
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

examples Issue was opened via the examples template.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants