我在根文件夹中有api(net core(和react应用程序项目。我从react应用程序开始对我的项目进行码头化。若docker compose和docker文件在同一个react应用程序文件夹中,则dockerize工作正常。但我想在未来从根文件夹启动两个docker。这是我的文件夹结构:
root
├ client-app
│ ├ node_modules
│ ├ public
│ ├ src
│ │ ├ etc folders and files
│ ├ Dockerfile
│ ├ package.json
│ └ package-lock.json
├ api folder
└ docker-compose.yml
这是码头文件:
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
这是码头工人的作品:
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组成dockerize:
=> 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
如果我更改dockerfile中的文件夹,如下所示:COPY package.json./或COPY。然后docker无法安装包或构建项目,因为找不到文件。如何修复从根文件夹构建react应用程序?非常感谢。
尝试更改客户端应用程序的构建上下文:
version: "3.4"
services:
app:
build:
context: ./client-app/
dockerfile: client-app/Dockerfile
我解决了错误。需要更改Workdir/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