gpt4 book ai didi

node.js - Docker Nodejs多阶段构建错误。找不到距离

转载 作者:行者123 更新时间:2023-12-02 21:33:43 24 4
gpt4 key购买 nike

FROM node:14.9-stretch-slim AS build
WORKDIR /usr/src/app
COPY ./package.json ./
RUN yarn
COPY . .
CMD ["yarn", "run", "build"]

FROM nginx:alpine
COPY nginx.conf /etc/nginx/nginx.conf
COPY --from=build /usr/src/app/dist /usr/share/nginx/html
这是我的dockerfile。构建它时,我收到一条错误消息,提示找不到dist。
COPY failed: stat /var/lib/docker/overlay2/e397e0fd69733a25f53995c12dfbab7d24ce20867fede11886d49b9537976394/merged/usr/src/app/dist: no such file or directory
知道为什么会这样吗?
任何帮助将不胜感激。
谢谢。

最佳答案

这里的问题是在CMD上使用RUN而不是CMD ["yarn", "run", "build"]

FROM node:14.9-stretch-slim AS build
WORKDIR /usr/src/app
COPY ./package.json ./
RUN yarn
COPY . .
RUN yarn run build

FROM nginx:alpine
COPY nginx.conf /etc/nginx/nginx.conf
COPY --from=build /usr/src/app/dist /usr/share/nginx/html
这是 RUNCMD的文档
从有关 CMD的文档中:

The main purpose of a CMD is to provide defaults for an executing container. These defaults can include an executable, or they can omit the executable, in which case you must specify an ENTRYPOINT instruction as well.


从有关 RUN的文档中:

The RUN instruction will execute any commands in a new layer on top of the current image and commit the results. The resulting committed image will be used for the next step in the Dockerfile.


如您所见,运行容器时 CMD在运行时执行,但是 RUN命令用于构建docker镜像
下次如何调试?
在Dockerfile中,您基本上可以运行镜像的任何可执行部分。因此,要检查dist文件夹是否存在或其内容是什么,您可以使用 ls
FROM node:14.9-stretch-slim AS build
WORKDIR /usr/src/app
COPY ./package.json ./
RUN yarn
COPY . .
RUN yarn run build
RUN ls -la dist

FROM nginx:alpine
COPY nginx.conf /etc/nginx/nginx.conf
COPY --from=build /usr/src/app/dist /usr/share/nginx/html

关于node.js - Docker Nodejs多阶段构建错误。找不到距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63933226/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com