13

In my container, I have a folder that contains a relative symlink to a parent's parent subfolder:

$ docker run --name symlink-test ubuntu bash -c "mkdir -p /1/2; touch /1/2/a; ln -s ../../usr /1/2; touch /1/2/z; ls -l /1/2"                                               :(
total 4
-rw-r--r--. 1 root root 0 Mar  4 03:37 a
lrwxrwxrwx. 1 root root 9 Mar  4 03:37 usr -> ../../usr
-rw-r--r--. 1 root root 0 Mar  4 03:37 z

I want to copy the folder /1 to the host. However, I always get the following error:

$ docker cp symlink-test:/1/2
invalid symlink "/tmp/2/usr" -> "../../usr"
$ ls 2
a

Copying the files fails and docker cp aborts after it sees the symlink.

There are some Docker bugs related to this, but they are either fixed or were caused by something different:

I'm running Docker 1.10.2 on Fedora 23.

Is the above behavior of docker cp intended or is it a bug? If it is intended, what's the reasoning behind it?

morxa
  • 3,221
  • 3
  • 27
  • 43

4 Answers4

8

In my case, I get it to work by:

docker cp -L container:/path/to/file.png current/directory/file.png
Him Hah
  • 1,385
  • 14
  • 9
4

I don't know whether this behavior is intentional, but here's a workaround:

docker cp my-container:/path/to/dir - | tar -x
mhsmith
  • 6,675
  • 3
  • 41
  • 58
4

You could docker exec -it <container> sh then find the symlink of the file you want to copy readlink -f symlinkName and then docker cp that one instead.

dimisjim
  • 368
  • 2
  • 19
0

The "docker cp -L " will fail on relative symlinks, you should fix them to be full path instead.

e.g. invalid symlink "/tmp/2/usr" -> "../../usr" usr is a relative symlink to ../../usr. instead change it to: usr -> /full/path/to/usr/

another example: invalid symlink "/working_folder/XXX/lib" -> "../../lib"

because: lib -> ../../lib/ fixed it: lib -> /full/path/to/lib/

and it will work by: docker cp -L /full/path/to/working_folder:/working_folder/

soll soll
  • 17
  • 2