Dockerize and Deploy a React/Next.js App
Create a production-optimized Docker image for a Next.js application with multi-stage builds and minimal image size.
Prerequisites
- -Docker installed
- -Next.js project with working build
Steps
Create a multi-stage Dockerfile
Write a Dockerfile that uses separate stages for dependencies, building, and the final runtime image.
Enable standalone output in next.config.ts: output: 'standalone' for a minimal production server.
Add a .dockerignore file
Exclude unnecessary files from the Docker build context to speed up builds.
Enable standalone output in Next.js config
Configure Next.js to produce a self-contained output that does not need node_modules.
Without standalone output, the final Docker image will include all of node_modules and be significantly larger.
Build the Docker image
Build the production image with a descriptive tag.
Run the container
Start the container and map port 3000.
The standalone image is typically 100-150MB compared to 1GB+ without multi-stage builds.
Full Script
FAQ
Discussion
Loading comments...