我正在尝试构建我的 React 应用程序,但是,出于项目的目的,Dockerfile 必须位于 compose/cloud/ 文件夹中(不能位于根目录中),这是我的文件夹结构
my-frontend-app/
│
├── compose/
│ └── cloud/
│ └── Dockerfile
│
├── src/
│ ├── components/
│ │ └── ...
│ ├── pages/
│ │ └── ...
│ ├── App.js
│ ├── index.js
│ └── ...
│
├── public/
│ ├── index.html
│ └── ...
│
├── package.json
├── package-lock.json
这是我的dockerfile
FROM node:18-alpine as build-stage
WORKDIR /app
RUN cd . .
RUN cd . .
COPY . compose/cloud/
RUN npm install
RUN npm run build
FROM nginx:alpine
RUN rm -rf /usr/share/nginx/html/*
COPY --from=build-stage /app/build /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
每当我尝试构建图像时,我都会收到此错误(我运行命令从 Dockerfile 所在的云文件夹中构建图像)
#9 [build-stage 3/7] RUN cd . .
#9 DONE 0.3s
#10 [build-stage 4/7] RUN cd . .
#10 DONE 0.4s
#11 [build-stage 5/7] COPY . compose/cloud/
#11 DONE 0.0s
#12 [build-stage 6/7] RUN npm install
#12 1.414 npm ERR! code ENOENT
#12 1.414 npm ERR! syscall open
#12 1.415 npm ERR! path /app/package.json
#12 1.416 npm ERR! errno -2
#12 1.422 npm ERR! enoent Could not read package.json: Error: ENOENT: no such file or directory, open '/app/package.json'
#12 1.422 npm ERR! enoent This is related to npm not being able to find a file.
我的 dockerfile 应该是什么?谢谢
Dockerfile 只能引用与构建上下文相关的文件。构建上下文是指定为
docker build
最终参数的目录。通常,Dockerfile 位于构建上下文的顶层,但您可以通过 -f
选项使用位于其他位置的 Dockerfile。
使用
compose/cloud/Dockerfile
中的 Dockerfile,您可以从顶级目录运行:
docker build -t my_image_tag -f compose/cloud/Dockerfile .
因为您的构建上下文是顶级目录,所以您可以执行以下操作:
COPY package.json /app/package.json
或:
COPY compose/cloud/ compose/cloud/
等等