In my Dockerfile, I'm trying to copy a directory from the host into the image. What I noticed if I use a relative directory, like this:
COPY ./foo/bar /
This works fine.
This does not:
COPY /foo/bar /
The error I get back is confusing when I try to build the image, too:
lstat /foo/bar: no such file or directory
...because if I just do an ls /foo/bar
on the host, it's there.
Is there some syntax I have wrong, or something else?
CodePudding user response:
Another user answered in a comment. Read the docker build documentation
The docker build command builds Docker images from a Dockerfile and a “context”. A build’s context is the set of files located in the specified PATH or URL. The build process can refer to any of the files in the context. For example, your build can use a COPY instruction to reference a file in the context.
In layman's terms: you can only perform the COPY command with paths relative to the root where your Dockerfile is located at.
Say you have this directory structure:
somefile.txt
project/
Dockerfile
config/configfile
data/datafile
In your docker file you can specify paths relative to the project directory, but cannot access anything above the root directory. somefile.txt is not accessible from the Dockerfile.