简体   繁体   English

如何对接 React Nextjs

[英]How to dockerize React Nextjs

I want to build my next js project by docker tool, but I got some trouble like this:我想通过 docker 工具构建我的下一个 js 项目,但是遇到了一些麻烦:

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: 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 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.当您将当前目录 map 到 /var/app 时,容器中该目录中的所有文件都将隐藏并替换为当前目录中的文件。

Since you don't have a.next directory in the host directory, the container can't find the built files.由于您在主机目录中没有 .next 目录,因此容器无法找到构建的文件。

To get it to run, you need to remove the mapping of the current directory, so your docker-compose file becomes要让它运行,您需要删除当前目录的映射,因此您的 docker-compose 文件变为

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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