Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker does not follow symlinks within build directory

I am Dockerizing an application which involves linking binaries with other C files via Clang. We maintain a symlinked version of the binaries since they are used throughout the codebase. My Docker build directory contains this entire codebase (including the source files as well as the symlinks to those source files), and Docker recognizes those files when I do things like cat [symlinked_file] (i.e. the file is cated properly). However, it fails to link the symlinked files when I run my Clang commands in my Makefile (these work fine not in Docker). I then copied the originals into the directory where the symlinks are, replacing the symlinks, and Docker did not throw any errors on build.

Does anyone know how to get around this? Are there any special commands I need to give to Docker or Clang here? I am not sure why Clang is behaving differently inside a Docker container than outside of it. I am running from an Ubuntu 16.04 Base Image for reference.

like image 536
Aadil Bhatti Avatar asked Sep 19 '25 15:09

Aadil Bhatti


1 Answers

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 -al
total 8
drwxr-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.out
lrwxrwxrwx  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 Dockerfile
FROM busybox
COPY . /build-context
WORKDIR /build-context
CMD find .

$ docker build -t test-symlink .
Sending build context to Docker daemon 3.584 kB
Step 1/4 : FROM busybox
 ---> 7968321274dc
Step 2/4 : COPY . /build-context
 ---> 8238dac16669
Removing intermediate container dd653dfdf7a4
Step 3/4 : WORKDIR /build-context
 ---> c1850cb52f0e
Removing intermediate container 7ee87e20d525
Step 4/4 : CMD find .
 ---> Running in e710e965d98c
 ---> fd57eb8f426b
Removing intermediate container e710e965d98c
Successfully built fd57eb8f426b

$ docker run test-symlink
.
./outside.txt
./Dockerfile
./source.txt
./target.txt

$ docker run -it --rm test-symlink /bin/sh
/build-context # ls -al
total 12
drwxr-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 Dockerfile
lrwxrwxrwx    1 root     root            11 Mar  3 02:08 outside.txt -> ../test.out
lrwxrwxrwx    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.txt
cat: can't open 'outside.txt': No such file or directory
/build-context # cat target.txt
/build-context # exit
like image 191
BMitch Avatar answered Sep 22 '25 07:09

BMitch