Docker Nodejs多阶段构建错误找不到距离 下次如何调试?

问题描述

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的文档中:

CMD的主要目的是为执行中的容器提供默认值。这些默认值可以包含可执行文件,也可以省略可执行文件,在这种情况下,您还必须指定一条ENTRYPOINT指令。

有关RUN的文档中:

RUN指令将在当前图像顶部的新层中执行所有命令,并提交结果。生成的提交映像将用于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