Docker does not follow symlinks within build directory Docker does not follow symlinks within build directory docker docker

Docker does not follow symlinks within build directory


Docker will work with symlinks (at least when building on my linux host), but what it copies is the symlink itself, without following the link. Therefore you also need the the symlink target to be separately included in the build context and copied into the image. This context is sent over to the docker engine and the build occurs on that server within containers, so any link to files outside of that context will not resolve. You will also want these links to be relative, or the absolution path of a link must point to the same absolute path inside the image. Here's an example with relative links showing the difference between files inside and outside the context:

$ ls -altotal 8drwxr-xr-x  2 bmitch bmitch 4096 Mar  2 21:08 .drwxr-xr-x 13 bmitch bmitch 4096 Mar  2 21:07 ..lrwxrwxrwx  1 bmitch bmitch   11 Mar  2 21:08 outside.txt -> ../test.outlrwxrwxrwx  1 bmitch bmitch   10 Mar  2 21:08 source.txt -> target.txt-rw-r--r--  1 bmitch bmitch    0 Mar  2 21:08 target.txt$ cat DockerfileFROM busyboxCOPY . /build-contextWORKDIR /build-contextCMD find .$ docker build -t test-symlink .Sending build context to Docker daemon 3.584 kBStep 1/4 : FROM busybox ---> 7968321274dcStep 2/4 : COPY . /build-context ---> 8238dac16669Removing intermediate container dd653dfdf7a4Step 3/4 : WORKDIR /build-context ---> c1850cb52f0eRemoving intermediate container 7ee87e20d525Step 4/4 : CMD find . ---> Running in e710e965d98c ---> fd57eb8f426bRemoving intermediate container e710e965d98cSuccessfully built fd57eb8f426b$ docker run test-symlink../outside.txt./Dockerfile./source.txt./target.txt$ docker run -it --rm test-symlink /bin/sh/build-context # ls -altotal 12drwxr-xr-x    2 root     root          4096 Mar  3 02:09 .drwxr-xr-x   20 root     root          4096 Mar  3 02:09 ..-rw-r--r--    1 root     root            69 Mar  3 02:08 Dockerfilelrwxrwxrwx    1 root     root            11 Mar  3 02:08 outside.txt -> ../test.outlrwxrwxrwx    1 root     root            10 Mar  3 02:08 source.txt -> target.txt-rw-r--r--    1 root     root             0 Mar  3 02:08 target.txt/build-context # cat outside.txtcat: can't open 'outside.txt': No such file or directory/build-context # cat target.txt/build-context # exit


I faced the exact same issue recently. After a lot of research and testing, here's what I found from the Docker team...

https://github.com/docker/docker/issues/1676

Yes, we have chosen to not follow symlinks for docker build because of the inconsistent results that could occur building on different systems.

Basically, it's not a feature that's coming.

Here's the folder structure I had. Two repositories that sit at the same directory level.

/cms-code/cms-themes

I wanted to symlink the /public folder inside /cms-code to the /cms-themes folder. And upon building a Dockerfile inside /cms-code to COPY or ADD the public folder into the image. Unfortunately, all I got was a symlink copied into the image/container, but not any contents.

The Options as I see them:

  1. Create a bash/sh script to copy the files into public after bothrepositories are setup. [Not conducive to active development orrefresh upon file changes]
  2. Use Git's submodule system to move mycms-themes repo into the /public folder of the /cms-coderepository. [Not practical for my particular situation]
  3. Use docker-compose to set the /public folder in /cms-code to sync with my host /cms-themes directory. [This is ultimately what Ichose. It's still not ideal, but it does work. The CMS container(once running) sees all the contents of /public which are actuallythe files in /cms-themes.

Hope this helps and is relevant to your situation. Let me know if you have any questions. I can share a sample docker-compose.yml file if you're unfamiliar with volumes in compose.

UPDATE -- Adding compose file reference as Gist:https://gist.github.com/sgelliott/191c681ebb261c6a36ecd5fb70eb0176