简体   繁体   中英

How to dockerize React Nextjs

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?

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM