Docker compose 无法纠正 build react app

Docker compose can't correct build react app

我在根文件夹中有 api (net-core) 和 react-app 项目。我从 React 应用程序开始 dockerize 我的项目。如果 docker-compose 和 docker 文件在同一个 react-app 文件夹中,dockerize 工作正常。但我希望将来从根文件夹开始两个 docker。这是我的文件夹结构:

root
      ├ client-app
      │  ├ node_modules
      │  ├ public
      │  ├ src
      │  │  ├ etc folders and files
      │  ├ Dockerfile
      │  ├ package.json
      │  └ package-lock.json
      ├ api folder
      └ docker-compose.yml

这是docker文件:

FROM node:alpine
WORKDIR /client-app
ENV PATH="./node_modules/.bin:$PATH"
COPY ./client-app/package.json /client-app/
RUN npm install
COPY ./client-app/ /client-app/
RUN npm run build

这是docker-撰写:

version: "3.4"
services:
  app:
    build:
      context: .
      dockerfile: client-app/Dockerfile
    volumes:
      - .:/client-app
    ports:
      - "3000:3000"
    image: app/react
    container_name: app_container
    command: npm start

然后我从根文件夹 docker 开始 docker- 编写:

 => exporting to image                                                                                                                           4.4s 
 => => exporting layers                                                                                                                          4.4s 
 => => writing image sha256:38172c4db5efba2b309f84f3c0b6c02e1ecbae89e611f1899205f663a805646e                                                     0.0s 
 => => naming to app/react                                                                                                                       0.0s 

Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
[+] Running 2/1
 - Network larchkiktest_default     Created                                                                                                      0.0s 
 - Container app_container  Created                                                                                                              0.1s
Attaching to app_container
app_container  | npm ERR! code ENOENT
app_container  | npm ERR! syscall open
app_container  | npm ERR! path /client-app/package.json
app_container  | npm ERR! errno -2
app_container  | npm ERR! enoent ENOENT: no such file or directory, open '/client-app/package.json'
app_container  | npm ERR! enoent This is related to npm not being able to find a file.
app_container  | npm ERR! enoent
app_container  |
app_container  | npm ERR! A complete log of this run can be found in:
app_container  | npm ERR!     /root/.npm/_logs/2022-04-10T18_00_24_224Z-debug-0.log
app_container exited with code 254

如果我更改 docker 文件中的文件夹,如下所示: 复制 package.json ./ 或复制 . .然后 docker 安装包或构建项目失败,因为找不到文件。我如何修复根文件夹中的构建反应应用程序?谢谢。

尝试更改客户端应用程序的构建上下文:

version: "3.4"
services:
  app:
    build:
      context: ./client-app/
      dockerfile: client-app/Dockerfile

我解决了错误。需要更改工作目录 /usr/src/app。现在我的文件是这样的:

FROM node:alpine
WORKDIR /usr/src/app
ENV PATH="./node_modules/.bin:$PATH"
COPY ./client-app/package*.json .
RUN npm install
COPY ./client-app/ .
RUN npm run build

version: "3.4"
services:
  app:
    build:
      context: .
      dockerfile: client-app/Dockerfile
    volumes:
      - ./:/app
    ports:
      - "3000:3000"
    image: app/react
    container_name: app_container
    command: npm start