docker-compose: failed to solve: rpc error: code = Unknown desc = failed to compute cache key

When I run my dockerfile with docker build and docker run, it’s working great. But when I create a docker-compose.yml file to connect mongoDB I got an Error.

ERROR: failed to solve: rpc error: code = Unknown desc = failed to compute cache key: failed to walk /var/lib/docker/tmp/buildkit-mount285447202/backend: lstat /var/lib/docker/tmp/buildkit-mount285447202/backend: no such file or directory.

Here my docker-compose code:

version: "3"
services:   
  backend: 
    build: 
      context: ./docker
      dockerfile: Dockerfile.backend
    container_name: backend
    ports: 
      - '8081:8081'
    links:  
      - mongodb
  mongodb:
    container_name: mongodb
    image: mongo
    ports:
      - '27017:27017'

Here is my dockerfile code:

FROM node:14-alpine AS builder
WORKDIR /app
COPY backend/package.json backend/package-lock.json ./
RUN npm install

COPY backend/src/ ./src
COPY backend/.env ./
COPY backend/.babelrc ./
RUN mkdir -p build/src
RUN npm run build

FROM node:alpine AS runner
WORKDIR /app

RUN apk add --no-cache 
      chromium 
      nss 
      freetype 
      harfbuzz 
      ca-certificates 
      ttf-freefont

ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true 
    PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium-browser

RUN npm install puppeteer@10.0.0

RUN addgroup -S pptruser && adduser -S -g pptruser pptruser 
    && mkdir -p /home/pptruser/Downloads /app 
    && chown -R pptruser:pptruser /home/pptruser 
    && chown -R pptruser:pptruser /app

# USER pptruser
USER root

COPY --from=builder /app/package.json ./
COPY --from=builder /app/package-lock.json ./
COPY --from=builder /app/.env ./
COPY --from=builder /app/build ./build
COPY --from=builder /app/node_modules ./node_modules

EXPOSE 8081

CMD ["node", "build/src/index.js"]

I got ERROR at:

=> ERROR [builder 3/9] COPY backend/package.json backend/package-lock.json ./                                   0.0s 
 => CACHED [builder 4/9] RUN npm install                                                                         0.0s 
 => ERROR [builder 5/9] COPY backend/src/ ./src                                                                  0.0s 
 => ERROR [builder 6/9] COPY backend/.env ./                                                                     0.0s 
 => ERROR [builder 7/9] COPY backend/.babelrc ./                                                                 0.0s 

My directory:

|-docker-compose.yml
|-backend
| |-src
| |-package.json
| |-package-lock.json 
| |-build
| |-...
|-docker
| |-Dockerfile.backend
| |-Dockerfile.frontend
|....

Answer

This usually happens when the Dockerfile can’t find the folder, e.g.

Dockerfile:

FROM alpine:3.8
COPY backend /tmp

Execution:

$ docker build .
Sending build context to Docker daemon  2.048kB
Step 1/2 : FROM alpine:3.8
 ---> c8bccc0af957
Step 2/2 : COPY backend /tmp
COPY failed: stat /var/lib/docker/tmp/docker-builder736780062/backend: no such file or directory

Make sure the context folder(here yours is docker) to have the backend folder.

For you, the correct way should be:

version: "3"
services:
  backend:
    build:
      context: .
      dockerfile: docker/Dockerfile.backend