gpt4 book ai didi

node.js - Docker compose 无法正确复制文件来运行 React 应用程序

转载 作者:太空宇宙 更新时间:2023-11-03 22:04:49 25 4
gpt4 key购买 nike

我正在尝试在 docker 容器内运行 create-react-app ,并使用 docker-compose 自动化 docker 构建/运行,以最终添加其他容器,例如后端 + 数据库。在本地文件夹中运行 docker-compose 工作正常,但将上下文设置为该文件夹并在父目录中运行它会导致错误。

我尝试让容器列出当前文件,以便我可以查看 package.json 是否已正确复制,但 ls 和 bash 不在 Node 镜像或容器的路径中,因此它们无法正常运行.

docker-compose.yaml

version: '3.5'

services:

dashboard-serve:
container_name: dashboard
build:
context: ./React-Frontend
dockerfile: Dockerfile
volumes:
- '.:/app'
- '/app/node_modules'
ports:
- '3001:3000'
environment:
- NODE_ENV=development

docker 文件

FROM node:12.2.0-alpine

# set working directory
WORKDIR /app

# add `/app/node_modules/.bin` to $PATH
ENV PATH /app/node_modules/.bin:$PATH

# install and cache app dependencies
COPY package.json /app/package.json
RUN npm install
RUN npm install react-scripts@3.0.1 -g

# start app
CMD ["npm", "start"]

它运行并输出找不到 package.json 的错误

dashboard          | npm ERR! path /app/package.json
dashboard | npm ERR! code ENOENT
dashboard | npm ERR! errno -2
dashboard | npm ERR! syscall open
dashboard | npm ERR! enoent ENOENT: no such file or directory, open '/app/package.json'
dashboard | npm ERR! enoent This is related to npm not being able to find a file.
dashboard | npm ERR! enoent
dashboard |
dashboard | npm ERR! A complete log of this run can be found in:
dashboard | npm ERR! /root/.npm/_logs/2019-07-30T15_55_23_780Z-debug.log
dashboard exited with code 254

最佳答案

您必须将文件复制到 Docker 容器中。目前,您仅复制 package.json 文件。

FROM node:12.2.0-alpine

# set working directory
WORKDIR /app

# add `/app/node_modules/.bin` to $PATH
ENV PATH /app/node_modules/.bin:$PATH

# install and cache app dependencies
COPY ./React-Frontend/package.json /app/package.json
RUN npm install
RUN npm install react-scripts@3.0.1 -g

# Copy files into Docker container
COPY ./React-Frontend /app

# start app
CMD ["npm", "start"]

关于node.js - Docker compose 无法正确复制文件来运行 React 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57275687/

25 4 0
文章推荐: node.js - 运行 Angular 项目会出现 Node-sass 错误
文章推荐: html - 使用 Css3 的简单动画
文章推荐: html - 如何设置或标签的高度?