Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker and symlinks

Tags:

docker

symlink

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?

like image 483
Chris B. Avatar asked Sep 07 '16 18:09

Chris B.


People also ask

Does docker work with symlinks?

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.

How do I mount a file in a docker container?

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 .

What is difference between ADD and copy in Dockerfile?

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.

What is docker build context?

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.


1 Answers

Docker doesn't support symlinking files outside the build context.

Here are some different methods for using a shared file in a container:

Share a base image

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 Dockerfiles

FROM worker-config:latest 

Build script

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 "$@" . 

Build from URL

Pull the config from a common URL for all worker-n builds.

ADD http://somehost/config.json / 

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.

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.

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.

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 

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.

docker create --name wcc -v /config worker-config /bin/true docker run --volumes-from wcc worker-a 

Mount config file from host

docker run -v /app/config/config.json:/config.json worker-a 
like image 50
Matt Avatar answered Nov 05 '22 10:11

Matt