How can I include files from outside of Docker's build context using the "ADD" command in the Docker file?
From the Docker documentation:
The path must be inside the context of the build; you cannot ADD ../something/something, because the first step of a docker build is to send the context directory (and subdirectories) to the docker daemon.
I do not want to restructure my whole project just to accommodate Docker in this matter. I want to keep all my Docker files in the same sub-directory.
Also, it appears Docker does not yet (and may not ever) support symlinks: Dockerfile ADD command does not follow symlinks on host #1676.
The only other thing I can think of is to include a pre-build step to copy the files into the Docker build context (and configure my version control to ignore those files). Is there a better workaround for than that?
I spent a good time trying to figure out a good pattern and how to better explain what's going on with this feature support. I realized that the best way to explain it was as follows...
So, with that said, here's an example of the Dockerfile that needs to reuse a file called
start.sh
Dockerfile
It
ALWAYS
will load from its relative path, having the current dir of itself as thelocal
reference to the paths you specify.Files
Considering this idea, we can think of having multiple copies for the Dockerfiles building specific things, but they all need access to the
start.sh
.Considering this structure and the files above, here's a docker-compose.yml
docker-compose.yaml
shared
context dir is theruntime
dir.context
.dockerfile
.The
docker-compose.yml
is as followsall-service
is set as the context, the shared filestart.sh
is copied there as well the Dockerfile specified by eachdockerfile
.Cheers!
I often find myself utilizing the
--build-arg
option for this purpose. For example after putting the following in the Dockerfile:You can just do:
The best way to work around this is to specify the Dockerfile independently of the build context, using -f.
For instance, this command will give the ADD command access to anything in your current directory.
Update: Docker now allows having the Dockerfile outside the build context (fixed in 18.03.0-ce, https://github.com/docker/cli/pull/886). So you can also do something like
If you read the discussion in the issue 2745 not only docker may never support symlinks they may never support adding files outside your context. Seems to be a design philosophy that files that go into docker build should explicitly be part of its context or be from a URL where it is presumably deployed too with a fixed version so that the build is repeatable with well known URLs or files shipped with the docker container.
Just my opinion but I think you should restructure to separate out the code and docker repositories. That way the containers can be generic and pull in any version of the code at run time rather than build time.
Alternatively, use docker as your fundamental code deployment artifact and then you put the dockerfile in the root of the code repository. if you go this route probably makes sense to have a parent docker container for more general system level details and a child container for setup specific to your code.
You can also create a tarball of what the image needs first and use that as your context.
https://docs.docker.com/engine/reference/commandline/build/#/tarball-contexts
On Linux you can mount other directories instead of symlinking them
See https://superuser.com/questions/842642 for more details.
I don't know if something similar is available for other OSes. I also tried using Samba to share a folder and remount it into the Docker context which worked as well.