Docker compose fail to start container in monorepo environment

Advertisements

I have a PNPM workspace environment. I try to build one of my package using docker compose.

To do so, I have the following script in package.json file (in root folder):
"cluster:start": "pnpm exec ./docker/scripts/start-cluster.sh"

This is the ./docker/scripts/start-cluster.sh file:

#!/bin/bash

docker-compose up -d

This is my docker-compose.yaml file:

version: '3.8'

services:
frontend:
    container_name: frontend
    build:
        context: ./docker
        dockerfile: Dockerfile.frontend-dev
    env_file:
        - ./apps/frontend/.env.development
    ports:
        - 8080:8080
    restart: always
    networks:
        - dashboard_network
networks:
    dashboard_network:
        driver: bridge

And this is my ./docker/Dockerfile.frontend-dev file:

FROM node:16

RUN curl -f https://get.pnpm.io/v6.16.js | node - add --global pnpm

WORKDIR /dashboard

COPY ../package.json ./
COPY ../apps/frontend/package.json ./apps/frontend/

RUN pnpm install

COPY ../ ./

CMD ["pnpm", "--filter", "frontend", "start"]

So when I run on root folder pnpm cluster:start I get an error:

=> ERROR [dashboard-frontend 4/7] COPY ../package.json ./                                                                                                          0.0s
 => ERROR [dashboard-frontend 5/7] COPY ../apps/frontend/package.json ./apps/frontend/                                                                              0.0s
------
 > [dashboard-frontend 4/7] COPY ../package.json ./:
------
------
 > [dashboard-frontend 5/7] COPY ../apps/frontend/package.json ./apps/frontend/:
------
failed to solve: failed to compute cache key: "/apps/frontend/package.json" not found: not found

Could anyone tell why?

This is the path to this error file: ROOT_FOLDER/apps/frontend/package.json

>Solution :

Short answer to "why": docker context.

This command is wrong:

COPY ../package.json ./

You cannot get "out" of the docker build context.

In your docker-compose.yml you define a context. That is a folder. When the build starts, the contents of that folder (with exceptions defined in .dockerignore) are sent to the docker daemon. And that is the set of file that your COPY commands can see. Nothing else. So the COPY does not work in your current folder, but on a copy of that folder that was sent to the docker daemon.

Leave a ReplyCancel reply