In this blog post, we will learn how to use Next.js with Docker.
Step 1: Create a Next.js app
First, create a new Next.js app using the following command:
npx create-next-app@latest my-next-app
Step 2: Create a Dockerfile
Next, create a Dockerfile
in the root of your project with the following content:
FROM node:lts
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "run", "dev"]
Step 3: Build the Docker image
Now, build the Docker image using the following command:
docker build -t my-next-app .
Step 4: Run the Docker container
Finally, run the Docker container using the following command:
docker run -p 3000:3000 my-next-app
Step 5: Docker compose configuration (optional)
If you want to use docker compose, you can create a docker-compose.yml
file with the following content:
services:
my-next-app:
container_name: my-next-app
restart: always
build: .
ports:
- "3000:3000"
Then, you can run the Docker container using the following command:
docker compose up
That's it! You have successfully used Next.js with Docker.