Dockerfile COPY command fails intermittently - Cannot find pyproject.toml file during build

I’m having weird issues with my Docker builds in a monorepo setup. Sometimes the build works fine, but other times it fails randomly with COPY errors for different services.

The error I keep getting looks like this:

=> ERROR [user-service builder 3/6] COPY requirements.txt package.json* ./                                                                                                                                                            0.0s
=> CANCELED [order-service internal] load build definition from Dockerfile                                                                                                                                                           0.1s
=> CANCELED [payment-service internal] load build definition from Dockerfile                                                                                                                                                          0.1s
=> ERROR [notification-service internal] load build context

This happens randomly - first build might work, second fails, then third works again. I have all the config files in the right place but Docker can’t seem to find them consistently.

Here’s my docker-compose service config:

notification-service:
    <<: *node-service
    build:
      context: .
      dockerfile: ./services/notification_app/Dockerfile
    ports: ["3001:3001"]
    depends_on: [redis, mongodb, rabbitmq]

And my Dockerfile:

FROM node:18-alpine AS deps

WORKDIR /app

RUN npm install -g yarn

COPY requirements.txt package.json ./

RUN yarn config set network-timeout 600000 && yarn install --frozen-lockfile --production=false

FROM node:18-alpine AS runner

WORKDIR /app

RUN addgroup -g 1001 -S nodegroup && adduser -S nodeuser -u 1001

COPY --from=deps /app/node_modules ./node_modules
COPY app/ .

RUN chown -R nodeuser:nodegroup /app
USER nodeuser
EXPOSE 3001
CMD ["node", "notification_app/server.js"]

Why does this keep happening? The files are definitely there in my project structure. Any ideas how to fix this inconsistent behavior?

I’ve encountered this issue with monorepos in the past. The problem stems from a mismatch between your build context and the file paths. While context: . points to your root directory, your Dockerfile is located in ./services/notification_app/, which can lead to Docker being confused about the file locations, especially given caching scenarios.

You can resolve this in two ways: either move the Dockerfile to the root and adjust the paths accordingly, or change the context to ./services/notification_app and update the copy commands in your Dockerfile. Additionally, I noticed you’re trying to copy requirements.txt into a Node.js container; ensure that this file is indeed present where Docker expects it to be. These intermittent errors likely indicate path resolution issues rather than actual missing files.