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.
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 containtest_input.txt
, thus it is not uploaded and docker can't find the file/There are two ways to solve this:
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:sudo docker build -t myimage .
should work.Because test_input.txt is in another directory,either put this file in place where dockerfile is there similar to test_output.txt or give full path in ADD command
Even for logfile.log this u will get error , so change to
Also make sure to not add in the
.dockerignore file
a file or folder that is needed during the image building process.