简体   繁体   English

Docker 撰写未正确复制文件以运行反应应用程序

[英]Docker compose not properly copying files to run react app

I am trying to run a create-react-app inside of a docker container and automate docker build/run with docker-compose to eventually add other containers like a backend + db.我正在尝试在 docker 容器内运行 create-react-app,并使用 docker-compose 自动化 docker build/run 以最终添加其他容器,如后端 + db。 Running docker-compose in the local folder works properly but setting the context to the folder and running it in the parent directory results in an error.在本地文件夹中运行 docker-compose 可以正常工作,但是将上下文设置为文件夹并在父目录中运行它会导致错误。

I have tried to get the container to list the current files so I can see if package.json is properly being copied over but ls and bash aren't in path of the node image or container so they won't run properly.我试图让容器列出当前文件,以便我可以查看 package.json 是否被正确复制,但 ls 和 bash 不在节点映像或容器的路径中,因此它们将无法正常运行。

docker-compose.yaml 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

dockerfile文件

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"]

It runs and outputs an error that it cannot find package.json它运行并输出找不到 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

You must copy your files into the Docker container.您必须将文件复制到 Docker 容器中。 Currently , you're only copying the package.json file.目前,您只复制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"]

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

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