This Dockerfile provides a secure and optimized way to build and run a Next.js application using a multi-stage build and a distroless final image.
- Builds a Next.js app using a minimal Node.js Alpine image.
- Installs dependencies conditionally based on the available lockfile (
yarn
,npm
, orpnpm
). - Runs the app on a distroless base image for a smaller attack surface and better security.
- Supports Next.js standalone output for efficient deployment.
base
– A minimal Node.js Alpine base image.deps
– Installs dependencies depending on the package manager.builder
– Copies code, runs the Next.js build, and prepares the standalone output.runner
– A secure distroless image that contains only the necessary files to run the app.
Distroless images:
- Contain only your app and runtime dependencies.
- Exclude shells, package managers, or OS tools.
- Reduce image size and improve security.
Expose port 3000 (default for Next.js):
docker build -t my-next-app .
docker run -p 3000:3000 my-next-app
You can also run the example app by using the ./run.sh script
./run.sh