timsterc
timsterc

Reputation: 1033

Docker only recognizes relative directories, not absolute

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?

Upvotes: 2

Views: 1853

Answers (1)

Razvan Mahalean
Razvan Mahalean

Reputation: 56

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.

EDIT: As the The Fool pointed out in his comment in regards to context. Think of context as the root directory and docker cannot get out of.

In layman's terms: you can only perform the COPY command with paths relative to the context you set.

Say you have this directory structure:

<Dockerfile path>/
   Dockerfile

somefile.txt
<your context directory>/
  config/configfile
  data/datafile

And you use this docker build command:

docker build -f <Dockerfile path>/Dockerfile /<your context directory>

In your docker file you can specify paths relative to the directory, but cannot access anything above the context directory. somefile.txt is not accessible from the Dockerfile.

Upvotes: 2

Related Questions