Answer a question

I've received this error code while composing on the latest docker desktop version 4.4.4 for windows: 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

This is my docker-compose.yml in the root of the all projects:

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

And this is the Dockerfile.prod which is in the frontend directory.

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;"

Here the entire error message

=> 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

This is my directory structure.

enter image description here

Answers

You need to RUN ng build in the build phase.

Setting a CMD (or ENTRYPOINT) doesn't actually do anything during an image build; it just sets the command that Docker will eventually run when it starts a container from the image. In a non-final stage of a multi-stage build, though, this never happens, Docker only copies files that already exist out of the image.

This simplified Dockerfile should work:

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
点击阅读全文
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐