COPYing symlinks into image

I’d like to use COPY to add a symlink to a container:

$ ls -l
-rw-r--r--  1 aaron  staff  22 Oct  9 22:54 Dockerfile
lrwxr-xr-x  1 aaron  staff   1 Oct  9 23:14 a -> b
-rw-r--r--  1 aaron  staff   0 Oct  9 23:14 b
$ cat Dockerfile
FROM alpine
WORKDIR /demo
COPY a b
RUN ls -l
$ docker build . --no-cache
Sending build context to Docker daemon  3.072kB
Step 1/4 : FROM alpine
 ---> 76da55c8019d
Step 2/4 : WORKDIR /demo
 ---> 55cb5ad4e3b8
Removing intermediate container 231b2544b180
Step 3/4 : COPY a b
 ---> 484f1310e25c
Step 4/4 : RUN ls -l
 ---> Running in 1d309baa3b80
total 0
-rw-r--r--    1 root     root             0 Oct 10 03:14 b
 ---> 32811d3a3e80
Removing intermediate container 1d309baa3b80
Successfully built 32811d3a3e80

As you can see, it copies the target of the symlink, not the symlink itself. And, in fact, it throws an error if the target doesn’t exist:

$ docker build .
Sending build context to Docker daemon   2.56kB
Step 1/4 : FROM alpine
 ---> 76da55c8019d
Step 2/4 : WORKDIR /demo
 ---> Using cache
 ---> 55cb5ad4e3b8
Step 3/4 : COPY a b
COPY failed: stat /var/lib/docker/tmp/docker-builder820865348/b: no such file or directory

Does docker just not support copying symlinks? I have to make them dynamically with RUN ln -s or something?

1 Like