docker add command fails to add the path passed as argument to it

863 views Asked by At

I have a Dockerfile which I want to be built and pushed after jenkins maven build. I have these lines in my Dockerfile:

...
ARG MAIN_DIR
ADD $MAIN_DIR .
...

And I pass MAIN_DIR argument as follows:

docker build -t my.gitlab.com:4567/path/to/my/project/my-image-name:my-image-tag --build-arg MAIN_DIR=Development .

But I get:

ADD failed: stat /var/lib/docker/tmp/docker-builder832988213/Development: no such file or directory

EDIT 1:

COPY $MAIN_DIR . generates the same issue.

EDIT 2:

Current directory actually contains a directory named Development and I'm completely sure there is no .dockerignore file in the whole project.

1

There are 1 answers

0
Tarun Lalwani On BEST ANSWER

From our chat discussion the Dockerfile you are using is under the Development directory and you are using below to build the image

docker build -t my.gitlab.com:4567/path/to/my/project/my-image-name:my-image-tag --build-arg MAIN_DIR=Development --build-arg VERSION=1.0.0 path/to/Docker/context/dir

your pwd is

/home/jenkins/.jenkins/workspace/Project-Name 

So when you send the context directory as the one containing the Dockerfile, then only content of that directory is sent to the docker daemon as context. And the development folder is in the parent hierarchy. So you won't be able to access anything that is above or outside the context folder.

Solution is to use a context directory which is separate from the Dockerfile

docker build -t my.gitlab.com:4567/path/to/my/project/my-image-name:my-image-tag --build-arg MAIN_DIR=Development --build-arg VERSION=1.0.0 -f path/to/docker/directory/Dockerfile path/to/projectdirectory