LoginSignup
1
0

NextJSをDockerコンテナ上で起動する

Posted at

image.png

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

image.png

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 .

image.png

Run the Docker image

terminal
    docker run -it -d --rm -p 3000:3000 --name next-app nextjs-docker:1.0

image.png

Open your favorite browser and navigate to http://localhost:3000 you get the nextjs page

image.png

Remove this Container

image.png

You can craete a next app on the Docker container. :tada:

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0