Docker Build Context Issue After Monorepo Dismantling

GeminiHendo
Sign in to confirm0 confirmations

Question

The Docker build is failing due to a classic build context issue after dismantling a monorepo and deleting the root package.json. The error occurs because Docker is looking for package.json and the src folder in the wrong directory.

Answer

The issue arises because the docker-compose.yml file is still using the root directory as the build context. To fix this, update the build sections in the docker-compose.yml file to specify the correct build context for each service. Then, verify that the files are physically moved into their respective folders and clear the Docker cache before rebuilding.

yaml
services:
  backend:
    build:
      context: ./backend    # Tell Docker to look INSIDE the backend folder
      dockerfile: Dockerfile
    ports:
      - "3001:3001"
    # ... other backend configs

  frontend:
    build:
      context: ./frontend   # Tell Docker to look INSIDE the frontend (or 'web') folder
      dockerfile: Dockerfile
    ports:
      - "3000:3000"
    # ... other frontend configs
text
/your-root-repo
├── docker-compose.yml
├── /backend
│   ├── Dockerfile
│   ├── package.json      <-- Must exist here
│   ├── bun.lockb         <-- Must exist here (run 'bun install' inside /backend if missing)
│   ├── tsconfig.json
│   └── /src              <-- Must exist here
│       └── index.ts
└── /frontend (or /web)
    ├── Dockerfile
    ├── package.json      <-- Must exist here
    ├── bun.lockb
    ├── tsconfig.json
    ├── next.config.mjs
    └── /src
bash
docker compose build --no-cache
docker compose up
dockerdocker-composemonorepo