I’d like to instruct Docker to COPY
my certificates from the local /etc/
folder on my Ubuntu machine.
I get the error:
COPY failed: file not found in build context or excluded by
.dockerignore: stat etc/.auth_keys/fullchain.pem: file does not exist
I have not excluded in .dockerignore
How can I do it?
Dockerfile:
FROM nginx:1.21.3-alpine
RUN rm /etc/nginx/conf.d/default.conf
RUN mkdir /etc/nginx/ssl
COPY nginx.conf /etc/nginx/conf.d
COPY ./etc/.auth_keys/fullchain.pem /etc/nginx/ssl/
COPY ./etc/.auth_keys/privkey.pem /etc/nginx/ssl/
WORKDIR /usr/src/app
I have also tried without the dot
–> same error
COPY /etc/.auth_keys/fullchain.pem /etc/nginx/ssl/
COPY /etc/.auth_keys/privkey.pem /etc/nginx/ssl/
By placing the folder .auth_keys
next to the Dockerfile –> works, but not desireable
COPY /.auth_keys/fullchain.pem /etc/nginx/ssl/
COPY /.auth_keys/privkey.pem /etc/nginx/ssl/
9
Answers
The docker context is the directory the Dockerfile is located in. If you want to build an image that is one of the restrictions you have to face.
In this documentation you can see how contexts can be switched, but to keep it simple just consider the same directory to be the context. Note; this also doesn’t work with symbolic links.
So your observation was correct and you need to place the files you need to copy in the same directory.
Alternatively, if you don’t need to copy them but still have them available at runtime you could opt for a mount. I can imagine this not working in your case because you likely need the files at startup of the container.
@JustLudo’s answer is correct, in this case. However, for those who have the correct files in the build directory and still seeing this issue; remove any trailing comments.
Coming from a C and javascript background, one may be forgiven for assuming that trailing comments are ignored (e.g.
COPY my_file /etc/important/ # very important!
), but they are not! The error message won’t point this out, as of my version of docker (20.10.11).For example, the above erroneous line will give an error:
COPY failed: file not found in build context or excluded by .dockerignore: stat etc/important/: file does not exist
… i.e. no mention that it is the trailing
# important!
that is tripping things up.It’s also important to note that, as mentioned into the docs:
That is, if you’re running
build
like this:Any
COPY
orADD
, having no context, will throw the error mentioned or another similar:If you face this error and you’re piping your
Dockerfile
, then I advise to use-f
to target a customDockerfile
.(
.
set the context to the current directory)Here is a test you can do yourself :
Dockerfile_test
file, with this contentbuild
piping the testDockerfile
, see how it fails because it has no context:build
with-f
, see how the sameDockerfile
works because it has context:I merely had quoted the source file while building a windows container, e.g.,
COPY "file with space.txt" c:/some_dir/new_name.txt
Docker doesn’t like the quotes.
Check your docker-compos.yml, it might be changing the context directory.
I had a similar problem, with the only clarification: I was running Dockerfile with docker-compos.yml
This is what my Dockerfile looked like when I got the error:
This is part of my docker-compose.yml where I described my service.
My docker-compos.yml was changing the context directory. Then I wrote a new path in the Dockerfile and it all worked.
project structure
FWIW this same error shows up when running
gcloud builds submit
if the files are included in.gitignore
Have you tried doing a simlink with ln -s to the /etc/certs/ folder in the docker build directory?
Alternatively you could have one image that has the certificates and in your image you just COPY FROM the docker image having the certs.
build
command:Dockerfile
seems to run (i.e. the system seems to locate it and starts running it), I found I needed to have the home directory pre-defined above, for any copying to happen.Dockerfile
, I had the file copying like this:Let me share my solution
Here is the outline of my project
Now my docker command was to be fired from a directory outside of app and not inside app folder.
So this is what worked for me
Note the context which is generally specified as "." was now replaced with the "app" folder.