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?
For this, you need to utilize the docker build instruction with the “-t” option and the name of a new container for a symlink. We have named this container “symlink”. The “dot” in this instruction will automatically pick the “dockerfile” and build it. The output shows that symlink has been built and ready in docker.
If you use -v or --volume to bind-mount a file or directory that does not yet exist on the Docker host, -v will create the endpoint for you. It is always created as a directory. Therefore, it is always created as a directory because my docker host does not have $(pwd)/config.py .
COPY is a docker file command that copies files from a local source location to a destination in the Docker container. ADD command is used to copy files/directories into a Docker image. It only has only one assigned function. It can also copy files from a URL.
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.
Docker doesn't support symlinking files outside the build context.
Here are some different methods for using a shared file in a container:
Create a Dockerfile
for the base worker-config
image that includes the shared config/files.
COPY config.json /config.json
Build and tag the image as worker-config
docker build -t worker-config:latest .
Source the base worker-config
image for all your worker Dockerfile
s
FROM worker-config:latest
Use a script to push the common config to each of your worker containers.
./build worker-n
#!/bin/sh set -uex rundir=$(readlink -f "${0%/*}") container=$(shift) cd "$rundir/$container" cp ../config/config.json ./config-docker.json docker build "$@" .
Pull the config from a common URL for all worker-n
builds.
ADD http://somehost/config.json /
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.
cd .. docker build -f worker-a/Dockerfile .
All the source paths you reference in a Dockerfile
must also change to match the new build context:
COPY workerathing /app
becomes
COPY worker-a/workerathing /app
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. Note that only the .dockerignore
file from the base of the build context is referenced.
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.
docker volume create --name=worker-cfg-vol docker run -v worker-cfg-vol:/config worker-config cp config.json /config docker run -v worker-cfg-vol:/config:/config worker-a
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.
docker create --name wcc -v /config worker-config /bin/true docker run --volumes-from wcc worker-a
docker run -v /app/config/config.json:/config.json worker-a
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With