skip to Main Content

I have my own directory structure:

+projects
    +project_1
    +project_2

+node
    +container_1
        -Dockerfile

in my Dockerfile I copy project_1 to /var/www/project_1 directory but unfortunately it is not copied:

#Dockerfile

FROM node:17
WORKDIR /var/www/project_1
COPY ../../projects/project_1 /var/www/project_1
RUN npm install
CMD ["npm","run","start:dev"]

Error:

 => ERROR [3/4] COPY ../../projects/project_1 /var/www/project_1                                                                                                                                                                                                               0.0s
------
 > [3/4] COPY ../../projects/project_1 /var/www/project_1:
------
failed to solve: rpc error: code = Unknown desc = failed to compute cache key: "/projects/project_1" not found: not found

2

Answers


  1. Using external files/folders during compilation

    You can’t do it because docker creators want it that way and as the creators, they have good reasons: simplicity and or security.

    Docker at build time, will only work with files or folders which are at the same folder of the Dockerfile.

    There are some workarounds like launch the docker build... from one folder and specify the location of Dockerfile which is in another folder.

    Check these answer to get more details:

    Don’t use external files/folders

    At build time, you should not depend of external folders or files. You must only need of a git repository with the source code of one application. This is common, widely used and devops compatible.

    Any other dependency required at build time, should be by good engineering:

    • library: In your case a nodejs/npm package public or private
    • docker base image: You could create another image with required files/folder and the use it a inheritance with FROM acme-base-nodejs or in docker multi-stage
    Login or Signup to reply.
  2. Files that you want to COPY should be located within the Dockerfile directory and not outside (eg. parent).

    +projects
        +project_1
        +project_2
    
    +node
        +container_1
    +Dockerfile  # <-- move your Dockerfile to your project root.
    

    Update your files location in the Dockerfile:

    ...
    COPY ./projects/project_1 /var/www/project_1
    ...
    

    Build will success when run at the project root:

    docker build -t <my tag> .

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search