This repository provides a Docker setup for a Next.js application, using pnpm for package management. It includes a multi-stage Dockerfile to build and run a Next.js application efficiently.
- Multi-stage Docker Build: Reduces final image size and improves security by separating the build and runtime environments.
pnpmPackage Management: Usespnpmfor faster, more efficient dependency management.- Optional Public Directory: Handles the optional presence of the
publicdirectory gracefully.
Ensure you have the following installed on your local machine:
-
Clone the Repository
git clone https://github.com/your-username/nextjs-docker.git cd nextjs-docker -
Install Dependencies
If you haven't already installed dependencies using
pnpm, you can do so with:pnpm install
-
Build the Docker Image
To build the Docker image, use the following command:
pnpm run docker:build
-
Run the Docker Container
To build and run the Docker container, use:
pnpm run docker:start
This will map port 3000 from the container to port 3000 on your host machine.
- Base Image: Uses Node.js 20 on Alpine Linux.
- Builder Stage:
- Sets up the working directory.
- Copies
package.jsonandpnpm-lock.yamland installs dependencies usingpnpm. - Copies the source code from
srcdirectory and builds the Next.js application.
- Runner Stage:
- Sets up a lightweight runtime environment.
- Creates a system user and group for security.
- Copies the build artifacts and necessary files from the builder stage.
- Runs the Next.js application.
You can customize the Dockerfile for your specific needs by:
- Adjusting the
COPYcommands if your project structure differs. - Adding environment variables for secrets using
ARG. - Modifying
CMDif you use a different entry point for your application.
- Missing
publicDirectory: If thepublicdirectory is not present, Docker will proceed without it, and no build errors will occur. - Docker Errors: Ensure Docker and
pnpmare correctly installed and updated.
Feel free to contribute by creating pull requests or opening issues. For significant changes, please discuss them first by opening an issue.
This project is licensed under the MIT License.
For more details, check the Next.js documentation and Docker documentation.