Skip to content

kristiyan-velkov/docker-nextjs-sample

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

4 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🐳 Docker Next.js Sample Project

License: MIT Docker Next.js React React TypeScript

A comprehensive demonstration of containerizing a modern Next.js application using Docker for both development and production workflows. This project showcases industry best practices for front-end containerization, including standalone and static-export builds, development with live sync, and optimized production delivery.

Part of the official Docker Next.js sample guide.


✨ Features

  • πŸ”₯ Modern Next.js with App Router and TypeScript
  • ⚑ Standalone & static export β€” Node server or static files (Nginx / serve)
  • 🎨 Tailwind CSS for utility-first styling
  • 🐳 Multi-stage Docker builds for optimized production images
  • πŸ”§ Development & production Docker configurations with Compose Watch
  • πŸ§ͺ Testing with Vitest and Testing Library
  • πŸ“¦ Docker Compose for easy orchestration
  • ☸️ Kubernetes deployment configuration
  • πŸ”’ Security-focused (non-root user, minimal base images)
  • πŸ“‹ ESLint for code quality
  • πŸ€– CI/CD with GitHub Actions (see .github/workflows)

πŸ› οΈ Tech Stack

Layer Technologies
Framework Next.js 16 (App Router), React 19, TypeScript
Styling Tailwind CSS v4
Testing Vitest, React Testing Library
Container Docker, Docker Compose
Orchestration Kubernetes (optional)
Web Server Nginx (static export), Node (standalone)

πŸ“‹ Prerequisites

  • Docker (v20.10+)
  • Docker Compose (v2.0+)
  • Node.js (v24+) β€” for local development
  • npm or yarn or pnpm β€” for local development

πŸš€ Quick Start

Using Docker (Recommended)

Clone the repository

git clone https://github.com/kristiyan-velkov/docker-nextjs-sample.git
cd docker-nextjs-sample

Development with Docker Compose

docker compose up nextjs-dev --build

Access the app at http://localhost:3000

Production (standalone)

docker compose up nextjs-prod-standalone --build

Access at http://localhost:3000

Production (static export + Nginx)

docker compose up nextjs-export --build

Access at http://localhost:8080

Note: Only one service using port 8080 (nextjs-export, nextjs-prod-static-nginx, or nextjs-prod-static-serve) should run at a time.

Local Development

Install dependencies

pnpm install
# or: npm install | yarn install

Start development server

pnpm dev

Run tests

pnpm run test:run

Build for production

pnpm build

🐳 Docker Commands

Development

# Build development image
docker build -f Dockerfile.dev -t nextjs-app-dev .

# Run development container
docker run -p 3000:3000 -v $(pwd):/app nextjs-app-dev

# Using Docker Compose (recommended, with watch)
docker compose watch nextjs-dev

Production (standalone)

# Build production image
docker build -t nextjs-sample:latest .

# Run production container
docker run -p 3000:3000 nextjs-sample:latest

Production (static export)

# Build static export + Nginx
docker build -f Dockerfile.export -t nextjs-export .
docker run -p 8080:8080 nextjs-export

One-off tasks (Compose, profile: tools)

docker compose --profile tools run --rm nextjs-test
docker compose --profile tools run --rm nextjs-lint

☸️ Kubernetes Deployment

Deploy the Next.js standalone app using the provided manifest:

# 1. Build the image (must match manifest)
docker build -t nextjs-sample:latest -f Dockerfile .

# 2. Apply the manifest (requires a running cluster, e.g. Docker Desktop K8s)
kubectl apply -f nextjs-sample-kubernetes.yaml

This creates:

  • Deployment (nextjs-sample) β€” 1 replica
  • Service (nextjs-sample-service) β€” NodePort 30001

Access: http://localhost:30001 (Docker Desktop) or use minikube service nextjs-sample-service --url for minikube.

Cleanup

kubectl delete -f nextjs-sample-kubernetes.yaml

See README.Docker.md for detailed Kubernetes steps (Docker Desktop, minikube, kind).


πŸ“ Project Structure

β”œβ”€β”€ app/                        # Next.js App Router
β”‚   β”œβ”€β”€ components/home/        # Home page components
β”‚   β”œβ”€β”€ data/                   # Author, resources data
β”‚   β”œβ”€β”€ layout.tsx
β”‚   β”œβ”€β”€ page.tsx
β”‚   └── globals.css
β”œβ”€β”€ public/                     # Static assets
β”œβ”€β”€ Dockerfile                   # Next.js standalone (production)
β”œβ”€β”€ Dockerfile.dev               # Next.js development
β”œβ”€β”€ Dockerfile.export            # Next.js static export β†’ Nginx
β”œβ”€β”€ Dockerfile.nginx             # Next.js static export β†’ Nginx (alt)
β”œβ”€β”€ Dockerfile.serve             # Next.js static export β†’ Node serve
β”œβ”€β”€ compose.yaml                 # Docker Compose configuration
β”œβ”€β”€ nginx.conf                   # Nginx config for static export
β”œβ”€β”€ nextjs-sample-kubernetes.yaml # Kubernetes deployment
β”œβ”€β”€ Taskfile.yml                 # Task automation
└── README.Docker.md             # Docker & Kubernetes guide

πŸ§ͺ Testing

# Local testing
pnpm run test:run

# Testing in Docker
docker compose --profile tools run --rm nextjs-test

πŸ›‘οΈ Security

This setup follows security best practices:

  • Non-root user (node / nginx) in production images
  • Minimal base images (slim / Alpine where applicable)
  • Lockfile-based installs for reproducible builds
  • Multi-stage builds to keep final image small

πŸ”§ Configuration

Environment Variables

Variable Description Default
PORT Application port 3000
NODE_ENV Environment mode development / production
HOSTNAME Next.js server host 0.0.0.0
NEXT_OUTPUT Next.js output mode standalone (or export in export Dockerfiles)

Docker Compose Override

Create compose.override.yaml for local customizations:

services:
  nextjs-dev:
    ports:
      - "3001:3000"
    environment:
      - CUSTOM_VAR=value

πŸ“š Available Scripts

Command Description
pnpm dev Start Next.js dev server
pnpm build Build for production
pnpm start Start production server
pnpm lint Run ESLint
pnpm run test:run Run tests once
pnpm test Run Vitest (watch)

🀝 Contributing

Contributions are welcome. Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

πŸ“ License

This project is licensed under the MIT License β€” see the LICENSE file for details.


πŸ‘¨β€πŸ’» Author

Kristiyan Velkov


β˜• Support

If you find this project helpful, consider supporting:


πŸ”— Related Resources

About

A production-ready and developer-friendly Docker setup for modern Next.js applications. Used in the official Docker Next.js sample to demonstrate secure, efficient, and scalable front-end containerization.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors