Home > Blockchain >  How to dockerize React Nextjs
How to dockerize React Nextjs

Time:11-29

I want to build my next js project by docker tool, but I got some trouble like this:

Error: Could not find a production build in the '/var/app/.next' directory. Try building your app with 'next build' before starting the production server. https://nextjs.org/docs/messages/production-start-no-build-id

Dockerfile:

FROM node:16-alpine
RUN mkdir -p /var/app
COPY ["./", "/var/app"]
WORKDIR /var/app
RUN npm i -g next
EXPOSE 3002
RUN npm run build
CMD ["npm", "run", "start"]

docker-compose.yml

version: '3.3'
services:
   next-project:
     container_name: next-project
     build: ./
     working_dir: /var/app
     restart: 'unless-stopped'
     volumes:
      - ./:/var/app
     env_file:
      - .env
     ports:
      - "54000:3002"

I do run commands like this

docker-compose build && docker-compose up -d

the build was successful but when it run is failed, is there any missing configuration?

CodePudding user response:

When you map your current directory to /var/app, all the files that are in that directory in the container become hidden and replaced with the files in the current directory.

Since you don't have a .next directory in the host directory, the container can't find the built files.

To get it to run, you need to remove the mapping of the current directory, so your docker-compose file becomes

version: '3.3'
services:
   next-project:
     container_name: next-project
     build: ./
     working_dir: /var/app
     restart: 'unless-stopped'
     env_file:
      - .env
     ports:
      - "54000:3002"
  • Related