Search code examples
dockerdocker-compose

docker-compose build errors on COPY


I'm running Docker Desktop on Windows 10. I have a folder on my desktop with the structure:

├── cab/
│ ├── backend/
│ │ ├── Dockerfile
│ │ └── (the rest of my backend java application) 
│ ├── frontend/
│ │ ├── Dockerfile
│ │ └── (the rest of my frontend react application) 
│ ├── mysql/
│ │ ├── Dockerfile
│ │ ├── schema.sql
│ │ └── data.sql
│ └── docker-compose.yml

My docker-compose.yml file looks like this:

services:
    mysql:
        build: 
            context: .
            dockerfile: ./mysql/Dockerfile
        ports:
            - "3306:3306"
        networks:
            - cab-booking
    cab-frontend:
        build: 
            context: .
            dockerfile: ./frontend/Dockerfile
        ports:
            - "3000:3000"
        networks:
            - cab-booking
    cab-backend:
        build: 
            context: .
            dockerfile: ./backend/Dockerfile
        ports:
            - "8080:8080"
        networks:
            - cab-booking

networks:
    cab-booking:

And this is my Dockerfile in frontend/:

FROM node:18-alpine
WORKDIR /home/cab-frontend

COPY package.json /home/cab-frontend/
RUN npm install

COPY public/ /home/cab-frontend/public
COPY src/ /home/cab-frontend/src

CMD ["npm", "start"]

When I run docker-compose build, I get this error output:

2024/05/06 15:12:58 http2: server: error reading preface from client //./pipe/docker_engine: file has already been closed
2024/05/06 15:12:58 http2: server: error reading preface from client //./pipe/docker_engine: file has already been closed
2024/05/06 15:12:58 http2: server: error reading preface from client //./pipe/docker_engine: file has already been closed
[+] Building 0.0s (0/0)  docker:default
2024/05/06 15:12:58 http2: server: error reading preface from client //./pipe/docker_engine: file has already been closed
2024/05/06 15:12:58 http2: server: error reading preface from client //./pipe/docker_engine: file has already been close[+] Building 0.9s (26/33)                                                                                docker:default
 => [cab-backend internal] load build definition from Dockerfile                                                   0.0s
 => => transferring dockerfile: 540B                                                                               0.0s
 => [mysql internal] load build definition from Dockerfile                                                         0.0s
 => => transferring dockerfile: 311B                                                                               0.0s
 => [cab-backend internal] load metadata for docker.io/library/openjdk:21-jdk-slim                                 0.8s
 => [cab-backend internal] load metadata for docker.io/library/maven:3.9.6-amazoncorretto-21-al2023                0.8s
 => [cab-frontend internal] load build definition from Dockerfile                                                  0.0s
 => => transferring dockerfile: 246B                                                                               0.0s
 => [mysql internal] load metadata for docker.io/library/mysql:8-oracle                                            0.0s
 => [mysql internal] load .dockerignore                                                                            0.0s
 => => transferring context: 2B                                                                                    0.0s
 => [cab-frontend internal] load metadata for docker.io/library/node:18-alpine                                     0.7s
 => CACHED [mysql 1/4] FROM docker.io/library/mysql:8-oracle                                                       0.0s
 => [mysql internal] load build context                                                                            0.0s
 => => transferring context: 369B                                                                                  0.0s
 => [mysql 2/4] COPY . .                                                                                           0.1s
 => [mysql 3/4] ADD schema.sql /docker-entrypoint-initdb.d                                                         0.1s
 => [mysql 4/4] ADD data.sql /docker-entrypoint-initdb.d                                                           0.1s
 => [mysql] exporting to image                                                                                     0.1s
 => => exporting layers                                                                                            0.1s
 => => writing image sha256:6da7f28843817809bf2083f8e5b70c979458f25465f8f22c148f507a0ae63667                       0.0s
 => => naming to docker.io/library/cab-mysql                                                                       0.0s
 => [cab-frontend internal] load .dockerignore                                                                     0.0s
 => => transferring context: 2B                                                                                    0.0s
 => [cab-backend internal] load .dockerignore                                                                      0.0s
 => => transferring context: 2B                                                                                    0.0s
 => [cab-frontend internal] load build context                                                                     0.0s
 => => transferring context: 2B                                                                                    0.0s
 => [cab-frontend 1/6] FROM docker.io/library/node:18-alpine@sha256:4837c2ac8998cf172f5892fb45f229c328e4824c43c85  0.0s
 => [cab-backend build 1/7] FROM docker.io/library/maven:3.9.6-amazoncorretto-21-al2023@sha256:8d653ed25358201bdb  0.0s
 => CANCELED [cab-backend internal] load build context                                                             0.1s
 => => transferring context: 136.38kB                                                                              0.0s
 => [cab-backend stage-1 1/2] FROM docker.io/library/openjdk:21-jdk-slim@sha256:7072053847a8a05d7f3a14ebc778a90b3  0.0s
 => CACHED [cab-frontend 2/6] WORKDIR /home/cab-frontend                                                           0.0s
 => ERROR [cab-frontend 3/6] COPY package.json /home/cab-frontend/                                                 0.0s
 => CACHED [cab-frontend 4/6] RUN npm install                                                                      0.0s
 => ERROR [cab-frontend 5/6] COPY public/ /home/cab-frontend/public                                                0.0s
 => ERROR [cab-frontend 6/6] COPY src/ /home/cab-frontend/src                                                      0.0s
------
 > [cab-frontend 3/6] COPY package.json /home/cab-frontend/:
------
------
 > [cab-frontend 5/6] COPY public/ /home/cab-frontend/public:
------
------
 > [cab-frontend 6/6] COPY src/ /home/cab-frontend/src:
------
failed to solve: failed to compute cache key: failed to calculate checksum of ref 2f1ad847-6c62-4f2a-91c3-e58ef7209d25::ibusmsuotmr2zt8ktmd9hc1dt: "/src": not found

I'm not sure what these errors mean - neither the http2 server error at the top of the output, nor the blank output for COPY, nor the "/src" not found. What do I do?


Solution

  • It turned out the error was with my context parameters. I had to change my docker-compose.yml file to look like this:

    services:
        mysql:
            build: 
                context: ./mysql
                dockerfile: ./Dockerfile
            ports:
                - "3306:3306"
            networks:
                - cab-booking
        cab-frontend:
            build: 
                context: ./frontend
                dockerfile: ./Dockerfile
            ports:
                - "3000:3000"
            networks:
                - cab-booking
        cab-backend:
            build: 
                context: ./backend
                dockerfile: ./Dockerfile
            ports:
                - "8080:8080"
            networks:
                - cab-booking
    
    networks:
        cab-booking:
    

    context: . would be fine if my project files were all sitting in the root directory where I ran docker-compose. But they're in the frontend directory.

    context: ./frontend is like telling docker compose to cd to frontend before running the Dockerfile. Since that's where the rest of my project files are, docker is then able to build the application.