Dockerコンテナを使ってNextJsアプリケーションを起動してみます。
Create a new Next.js app using create-next-app
terminal
mkdir nextjs-docker
cd nextjs-docker
npx create-next-app@latest .
Create a Dockerfile and .dockerignore file
terminal
touch Dockerfile
touch .dockerignore
Directory structure
Write the Dockeefile
Dockerfile
FROM node:20.2.0-alpine
WORKDIR /usr/src/app
COPY . /usr/src/app/
RUN npm install --production
RUN npm run build
EXPOSE 3000
CMD ["npm", "start"]
Write the .dockerignore
.dockerignore
node_modules
Build the Docker image
terminal
docker build -t nextjs-docker:1.0 .
Run the Docker image
terminal
docker run -it -d --rm -p 3000:3000 --name next-app nextjs-docker:1.0