无法与 Docker 和 "failed to walk" 组合

Failed to compose with Docker with "failed to walk"

我在 windows 的最新 docker 桌面版本 4.4.4 上撰写时收到此错误代码:docker-compose -f ./docker-compose.yml up

failed to solve: rpc error: code = Unknown desc = failed to compute cache key: failed to walk /var/lib/docker/tmp/buildkit-mount067465596/build/dist: lstat /var/lib/docker/tmp/buildkit-mount067465596/build/dist: no such file or directory

这是我的 docker-compose.yml 在所有项目的根目录中:

  version: "3.9"
    services: 
      frontend:
        build:
          context: frontend
          dockerfile: Dockerfile.prod
        ports: 
          - "80:80"
        environment:
          - NODE_ENV=production

这是前端目录中的 Dockerfile.prod。

ARG WORK_DIR=/build

#stage 1
FROM node:16-alpine as builder

ARG WORK_DIR
ENV PATH ${WORK_DIR}/node_modules/.bin:$PATH

RUN mkdir ${WORK_DIR}
WORKDIR ${WORK_DIR}

COPY package*.json ${WORK_DIR}

#RUN npm install -g npm@latest
RUN npm install @angular/cli
RUN npm install

COPY . ${WORK_DIR}

CMD ng build --prod

#stage 2
FROM nginx:latest

ARG WORK_DIR

#COPY --from=builder ${WORK_DIR}/dist/frontend /usr/share/nginx/html
COPY --from=builder ${WORK_DIR}/dist/frontend /usr/share/nginx/html
COPY ./nginx/nginx.conf /etc/nginx/conf.d/default.conf
EXPOSE 80
CMD nginx -g "daemon off;"

这里是完整的错误信息

=> ERROR [stage-1 2/3] COPY --from=builder /build/dist/frontend /usr/share/nginx/html   0.0s 
------
 > [stage-1 2/3] COPY --from=builder /build/dist/frontend /usr/share/nginx/html:
------
failed to solve: rpc error: code = Unknown desc = failed to compute cache key: failed to walk /var/lib/docker/tmp/buildkit-mount067465596/build/dist: lstat /var/lib/docker/tmp/buildkit-mount067465596/build/dist: no such file or directory

这是我的目录结构。

您需要在构建阶段RUN ng build

设置 CMD(或 ENTRYPOINT)在图像构建期间实际上没有做任何事情;它只是设置命令 Docker 最终会 运行 当它从图像启动容器时。但是,在 multi-stage 构建的 non-final 阶段,这永远不会发生,Docker 只会复制已存在于映像之外的文件。

这个简化的 Docker 文件应该可以工作:

FROM node:16-alpine as builder
WORKDIR /app          # also creates the directory
COPY package*.json .  # relative to the WORKDIR
RUN npm ci            # including devDependencies, like @angular/cli
COPY . .
RUN ./node_modules/.bin/ng build --production  # not CMD

FROM nginx:latest
COPY --from=builder /app/dist/frontend /usr/share/nginx/html
COPY ./nginx/nginx.conf /etc/nginx/conf.d/default.conf