Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker cp a folder with a relative symlink: invalid symlink

Tags:

docker

symlink

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:

  • FATA[0000] invalid symlink when copying a symlink with relative parent paths
  • Error attempting to cp directory containing symlink

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?

like image 880
morxa Avatar asked Mar 04 '16 03:03

morxa


2 Answers

In my case, I get it to work by:

docker cp -L container:/path/to/file.png current/directory/file.png
like image 58
Him Hah Avatar answered Sep 22 '22 09:09

Him Hah


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

docker cp my-container:/path/to/dir - | tar -x
like image 35
mhsmith Avatar answered Sep 20 '22 09:09

mhsmith