Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ADD failed : No such file/Directory while building docker image

Tags:

I have below Dockerfile:

FROM python:3  RUN mkdir -p /test/code/ RUN mkdir -p /test/logs/ RUN mkdir -p /test/configs/  ADD test.py /test/code/ ADD test_output.txt /test/code/ ADD test_input.txt /test/configs/ ADD logfile.log /test/logs/   CMD [ "python3", "/test/code/test.py" ] 

My directory structure is:

/home/<username>/test/                  |-> code/Dockerfile, test_output.txt, test.py                  |-> logs/logfile.log                  |-> configs/test_input.txt 

when I am building the docker image using below command:

sudo docker build -t myimage . 

It shows below error:

Step 7/9 : ADD test_input.txt /test/configs/ ADD failed: stat /var/lib/docker/tmp/docker-builder562406652/test_input.txt: no such file or directory 

Why it shows this error when I have the directory and my file is also present.

like image 1000
Anna Carolina Avatar asked Dec 26 '17 11:12

Anna Carolina


People also ask

What is Workdir in Dockerfile?

The WORKDIR command is used to define the working directory of a Docker container at any given time. The command is specified in the Dockerfile. Any RUN , CMD , ADD , COPY , or ENTRYPOINT command will be executed in the specified working directory.

Where is .dockerignore file located?

.dockerignore file. Before the docker CLI sends the context to the docker daemon, it looks for a file named .dockerignore in the root directory of the context.

When using copy with more than one source file the destination must be a directory and end with a?

According to the Dockerfile specification, if multiple sources are specified, then the destination must be a directory, and it must end with a slash '/'.

What should be in Dockerignore?

Usually, you put the Dockerfile in the root directory of your project, but there may be many files in the root directory that are not related to the Docker image or that you do not want to include. . dockerignore is used to specify such unwanted files and not include them in the Docker image.


2 Answers

This doesn't work because test_input.txt is not in the docker build context.

When you execute the command sudo docker build -t myimage . the last '.' indicates the build context. What docker does is that it uploads the context to the docker deamon to build the image. In your case the context does not contain test_input.txt, thus it is not uploaded and docker can't find the file/

There are two ways to solve this:

  1. Inside test directory run the command sudo docker build -t myimage -f code/Dockerfile .. In this case the context includes all the test directory. Then modify the Dockerfile to account for this change:

FROM python:3 ...     ADD code/test.py /test/code/ ADD code/test_output.txt /test/code/ ADD config/test_input.txt /test/configs/ ADD logs/logfile.log /test/logs/ 
  1. The second option is to simply move the Dockerfile to the test folder and modify it as above. In that case the command sudo docker build -t myimage . should work.
like image 93
yamenk Avatar answered Oct 03 '22 21:10

yamenk


Also make sure to not add in the .dockerignore file a file or folder that is needed during the image building process.

like image 35
soscler Avatar answered Oct 03 '22 19:10

soscler