Docker cp container:$path . fails

Hi every body,

Maybe this is not a 100% relevant question about docker, but I am having some troubles calling docker cp with a variable.

I am trying to automate some backup calls for my gitlab container. here is my test script:

#!/bin/bash
lastbackupfile=$(docker exec -t mygitlab ls /var/opt/gitlab/backups -tr | tail -n1)
#lastbackupfile="1513690222_2017_12_19_10.2.5_gitlab_backup.tar"
echo $lastbackupfile
pathA="mygitlab:/var/opt/gitlab/backups/"
path=$pathA$lastbackupfile
echo $path
docker cp $path /home/aqg

with the docker exec -t mygitlab ls /var/opt/gitlab/backups -tr | tail -n1 I am fetching the latest created backup from the /var/opt/gitlab/backups frolder.

so, the first echo shows me a correct file name: which is: 1513690222_2017_12_19_10.2.5_gitlab_backup.tar

the last echo shows: mygitlab:/var/opt/gitlab/backups/1513690222_2017_12_19_10.2.5_gitlab_backup.tar

now the last call. docker cp fails with the error:
Error response from daemon: lstat /var/lib/docker/aufs/mnt/48fb0a1f22abc2c8120018b94b5e9687c49471e722c02a6d76907e843f09cff8/var/opt/gitlab/backups/1513690222_2017_12_19_10.2.5_gitlab_backup.tar: no such file or directory

now, wenn I use the same script as above but this time switch the lastbackupfile assignments (just for clarification: lastbackfile is in both cases the same) - then the script work as expected.

in other words, if I make this shorter and just call the following in the terminal:
docker cp mygitlab:/var/opt/gitlab/backups/$(docker exec -t mygitlab ls /var/opt/gitlab/backups -tr | tail -n1) /home/aqg I get tge error as above mentioned.

and of course this works:
docker cp mygitlab:/var/opt/gitlab/backups/1513690222_2017_12_19_10.2.5_gitlab_backup.tar /home/aqg

so, I am guessing, that this is not a problem of the docker, but what am I doing wrong here?

many thanks in advance!

forget about this stupid post! I solved the Problem after some hours of playing around with bash scripting. As excepted, it was nothing to do with docker, it was just my little knowledge in bash scripting.

Just close or delete this thread!

Thanks.

Hello, i’m newbie to both bash and docker. I’m having exactly the same issue you mentioned, would you share how you fix this? thanks!

Hi,

Well the solution in my case was very simple. The output of bash call, in my case:
lastbackupfile=$(docker exec -t mygitlab ls /var/opt/gitlab/backups -tr | tail -n1) had included also “return” carriage. so I used the printf command with tr command to remove the return carriage character:

filename=$(printf $lastbackupfile | tr -d '\r')

I hope this will help you to solve your problem!

Cheers

1 Like

Thank you man, it works!