在 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
這是我的目錄結構。

uj5u.com熱心網友回復:
您需要RUN ng build在構建階段。
設定 a CMD(or ENTRYPOINT) 在影像構建程序中實際上并沒有做任何事情;它只是設定 Docker 在從映像啟動容器時最終將運行的命令。然而,在多階段構建的非最終階段,這永遠不會發生,Docker 只會復制已經存在于鏡像之外的檔案。
這個簡化的 Dockerfile 應該可以作業:
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/421325.html
標籤:
