20

I have a problem with pathways docker-compose, when I try build project with only docker build, it works great, but I mustn't use docker build, I have to use docker-compose. When I use docker-compose it returns 2 ERRORS at step 3/5 => ERROR [3/5] COPY /app/package.json . and at step 5/5 => ERROR [5/5] COPY /app .:

PS C:\Users\mamba\Desktop\project-practice> docker-compose -f docker/docker-compose.yml up -d
[+] Building 1.4s (9/9) FINISHED
 => [internal] load build definition from Dockerfile                                                                                                          0.1s 
 => => transferring dockerfile: 31B                                                                                                                           0.0s 
 => [internal] load .dockerignore                                                                                                                             0.1s 
 => => transferring context: 34B                                                                                                                              0.0s 
 => [internal] load metadata for docker.io/library/node:latest                                                                                                1.0s 
 => [1/5] FROM docker.io/library/node@sha256:c3356b2b11ad643852a321308c15d70ca2bc106e40d3ffe7a4879d3588a9d479                                                 0.0s 
 => [internal] load build context                                                                                                                             0.1s 
 => => transferring context: 2B                                                                                                                               0.0s 
 => CACHED [2/5] WORKDIR /app                                                                                                                                 0.0s 
 => ERROR [3/5] COPY /app/package.json .                                                                                                                      0.0s 
 => CACHED [4/5] RUN npm install                                                                                                                              0.0s 
 => ERROR [5/5] COPY /app .                                                                                                                                   0.0s 
------
 > [3/5] COPY /app/package.json .:
------
------
 > [5/5] COPY /app .:
------
failed to solve: rpc error: code = Unknown desc = failed to compute cache key: "/app/package.json" not found: not found

this is my project structure http://skrinshoter.ru/s/080721/upY64zwf

this is my Dockerfile

FROM node
WORKDIR /app
COPY /app/package.json .
RUN npm install
COPY /app .
EXPOSE 3000
CMD ["npm", "start"]

this is my docker-compose.yml

version: "3.8"
services: 
    react-app:
        working_dir: /app
        build: 
            dockerfile: Dockerfile
        ports: 
            - "3000:3000"
        volumes: 
            - ./app/src:/app/src
        environment: 
            - CHOKIDAR_USEPOLLING=true
        # env_file: 
        #     - ./docker/.env

If I move docker-compose.yml upper in structure of files to project-practice, it works great, it builds and server starts, but I have to keep structure of folders and files like this.

|-project-practice
|-app
|  |...
|-docker
   |...
f1x0z
  • 307
  • 1
  • 2
  • 6

3 Answers3

9

Looks like you mounting volumes wrong. Change your docker-compose configuration from:

build: 
     dockerfile: Dockerfile
volumes:
     - ./app/src:/app/src     

You mounting only the SRC folder, but you need files outside of it. Also you need to add context to your docker file

to:

build: 
     context: ../
     dockerfile: /docker/Dockerfile
volumes:
     - ../app:/app

The path should be relative to a docker-compose file location.

Also you need to modify Dockerfile:

FROM node
WORKDIR /app
COPY /app/package.json .
RUN npm install
COPY /app .
EXPOSE 3000
CMD ["npm", "start"]
rmik
  • 116
  • 4
  • I tryed to do as you say, but it still same problems :(( – f1x0z Jul 08 '21 at 20:36
  • only this changed now `failed to solve: rpc error: code = Unknown desc = failed to compute cache key: "/app" not found: not found` – f1x0z Jul 08 '21 at 20:43
  • but `=> ERROR [3/5] COPY /app/package.json .` and `=> ERROR [5/5] COPY /app .` not changed :( – f1x0z Jul 08 '21 at 20:44
  • Yep, got it. You need to modify Dockerfile. ```COPY ../app/package.json .``` and ```COPY ../app .``` – rmik Jul 08 '21 at 20:47
  • 1
    anyway, :((( not working, same thing( `=> ERROR [3/5] COPY ../app/package.json .` and `=> ERROR [5/5] COPY ../app .` and `failed to solve: rpc error: code = Unknown desc = failed to compute cache key: "/app/package.json" not found: not found` :(((((( – f1x0z Jul 08 '21 at 20:53
  • Paths in the `COPY` command need to be relative to the `build: { context: }` directory, and can't start with `..`. See also [How to include files outside of Docker's build context?](https://stackoverflow.com/questions/27068596/how-to-include-files-outside-of-dockers-build-context). – David Maze Jul 08 '21 at 23:00
  • i also faced issue but in my case node_module folder was present and I just delete it and it worked perfectly and hopefully I will help you too – Muhammad Irfan Aslam Feb 15 '22 at 07:52
  • adding the point to the nested directory did really fix my issue thanks a lot @f1x0z – Benderradji Khireddine Jan 09 '23 at 20:36
0

So if your yml is nested you need to use ../app/src:/app/src rather than the ./app/src:/app/src as that is it's relative location to the yml.

user3875913
  • 245
  • 2
  • 11
0

Although it is already answered already, for anyone getting this error for a different use case, this error corresponds to DockerFile rather than docker-compose. Check logs to see where this is failing.

Leena
  • 703
  • 1
  • 12
  • 21