I've got a repo set up like this:
/config
config.json
/worker-a
Dockerfile
<symlink to config.json>
/code
/worker-b
Dockerfile
<symlink to config.json>
/code
However, building the images fails, because Docker can't handle the symlinks. I should mention my project is far more complicated than this, so restructuring directories isn't a great option. How do I deal with this situation?
An alternative solution is to upgrade all your soft links into hard links.
Docker doesn't support symlinking files outside the build context.
Some different methods for using a shared file in a container.
Share a base image
Create a
Dockerfile
for the baseworker-config
image that includes the shared config/files.Build and tag the image as
worker-config
Source the base
worker-config
image for all your workerDockerfile
sBuild script
Use a script to push the common config to each of your worker containers.
./build worker-n
Build from URL
Pull the config from a common URL for all
worker-n
builds.Increase the scope of the image build context
Include the symlink target files in the build context by building from a parent directory that includes both the shared files and specific container files.
All the source paths you reference in a
Dockerfile
must also change to match the new build context:becomes
Using this method can make all build contexts large if you have one large build context, as they all become shared. It can slow down builds, especially to remote Docker build servers.
Mount a config directory from a named volume
Volumes like this only work as directories, so you can't specify a file like you could when mounting a file from the host to container.
Mount config directory from data container
Again, directories only as it's basically the same as above. This will automatically copy files from the destination directory into the newly created shared volume though.
Mount config file from host
The
docker build
CLI command sends the specified directory (typically.
) as the "build context" to the Docker Engine (daemon). Instead of specifying the build context as/worker-a
, specify the build context as the root directory, and use the-f
argument to specify the path to theDockerfile
in one of the child directories.You'll have to rework your Dockerfiles slightly, to point them to
../config/config.json
, but that is pretty trivial to fix.Also check out this question/answer, which I think addresses the exact same problem that you're experiencing.
How to include files outside of Docker's build context?
Hope this helps! Cheers