docker cp a folder with a relative symlink: invalid symlink
Asked Answered
O

4

14

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?

Ouzo answered 4/3, 2016 at 3:57 Comment(2)
What is the current WORKDIR in your container, when you are doing your docker cp?Retaliation
The current WORKDIR is the default WORKDIR of the Ubuntu image, which is /.Ouzo
C
10

In my case, I get it to work by:

docker cp -L container:/path/to/file.png current/directory/file.png
Cupcake answered 30/8, 2019 at 2:26 Comment(1)
In the docs, '-L' is described as -L, --follow-link Always follow symbol link in SRC_PATHAggi
H
5

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

docker cp my-container:/path/to/dir - | tar -x
Hepplewhite answered 15/12, 2018 at 21:13 Comment(0)
M
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.

Morph answered 28/7, 2019 at 15:27 Comment(1)
Thanks for the hint, but this doesn't answer the question.Ouzo
S
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/

Sakai answered 17/10, 2021 at 12:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.