Docker COPY issue - "no such file or directory"
Solution 1:
For me the directory was in the correct context, only it was included in the (hidden) .dockerignore
file in the root of the project. This leads to the error message:
lstat mydir/myfile.ext: no such file or directory
Solution 2:
From the documentation :
The
<src>
path must be inside the context of the build; you cannot COPY ../something /something, because the first step of a docker build is to send the context directory (and subdirectories) to the docker daemon.
When you use /srv/visitor
you are using an absolute path outside of the build context even if it's actually the current directory.
You better organize your build context like this :
├── /srv/visitor
│ ├── Dockerfile
│ └── resources
│ ├── visitor.json
│ ├── visitor.js
And use :
COPY resources /srv/visitor/
Note:
docker build - < Dockerfile
does not have any context.
Hence use,
docker build .
Solution 3:
For me the issue was that I was using docker build - < Dockerfile
From the documentation
Note: If you build using STDIN (docker build - < somefile
), there is no build context, so COPY can’t be used.
Solution 4:
As Xavier Lucas [extremely helpful] answer has stated, you cannot use COPY or ADD from a directory outside of your build context (the folder you run "docker build" from, should be the same directory as your .Dockerfile). Even if you try to use a symlink, it will not work.
Note: This is specific to POSIX (Linux, Unix, Mac, Possibly Linux Subsystem for Windows). You may be able to do similar in Windows using JUNCTION.
cd ~/your_docker_project/
cp -al /subfolder/src_directory ./
echo "COPY src_directory /subfolder/" >> Dockerfile
Danger: Using this will make your docker project specific to the host. You almost never want to do this! Handle with care.
Application: Learning, Experimenting in a Development Environment
This did the trick for me. cp -al copies the directory structure and makes hard links for all the files. When you are done run "rm -rf ./src_directory" to remove it.
Solution 5:
I was running into this issue and found out that I was able to add a context to the build variable in order to load my Dockerfile(s) from other directories. This allowed me to change my default Docker file structure a little more to my liking. Here is a snippet from my docker-compose.yml:
version: '3'
services:
webserver:
build:
context: .
dockerfile: ./server/Dockerfile
...
By adding the context I was able to define where the files should be referenced. You can reference the Docker docs here: https://docs.docker.com/compose/compose-file/#context
Hope this helps!