46

Creating a backup script to dump mongodb inside a container, I need to copy the folder outside the container, Docker cp doesn't seem to work with wildcards :

docker cp mongodb:mongo_dump_* .

The following is thrown in the terminal :

Error response from daemon: lstat /var/lib/docker/aufs/mnt/SomeHash/mongo_dump_*: no such file or directory

Is there any workaround to use wildcards with cp command ?

4 Answers 4

38

I had a similar problem, and had to solve it in two steps:

$ docker exec <id> bash -c "mkdir -p /extract; cp -f /path/to/fileset* /extract"
$ docker cp <id>:/extract/. .
4
  • 2
    This works well. 2 things: [1] mkdir will fail if used before, unless docker exec is performed with 'rm -rf /extract' (note this completely deletes the files moved inside the container). [2] may want to use 'cp" inside exec unless you want the files moved inside the container. Apr 15, 2019 at 18:53
  • 1
    Nice solution. use mkdir -p to get round the folder exists issue.
    – Martin
    Apr 29, 2020 at 15:19
  • 1
    handy pattern! note that if you just want the contents of extract/, and not the whole folder itself, you can add /. (forward slash followed by period), i.e.: docker cp <id>:/extract/. . (docs)
    – ellemenno
    Feb 13, 2021 at 4:41
  • I didn't even have to do the first step; seems like /. works like I was expecting /* to work, it even got dot files. Jul 17 at 5:02
28

It seems there is no way yet to use wildcards with the docker cp command https://github.com/docker/docker/issues/7710.

You can create the mongo dump files into a folder inside the container and then copy the folder, as detailed on the other answer here.

If you have a large dataset and/or need to do the operation often, the best way to handle that is to use docker volumes, so you can directly access the files from the container into your host folder without using any other command: https://docs.docker.com/engine/userguide/containers/dockervolumes/

0
5

Today I have faced the same problem. And solved it like:

docker exec container /bin/sh -c 'tar -cf - /some/path/*' | tar -xvf -

Hope, this will help.

0
1

Another possibility is to first get a list of the desired files with ls inside the container, which works with wildcards. Then pass each line of ls as part of the argument to docker cp in a while loop:

In your case:

docker exec container bash -c "ls mongo_dump_*" | while read line; do docker cp container:/$line .; done

Another simple example to copy all png files from /home inside the docker container to the current directory.

docker exec container bash -c "ls home/*.png" | while read line; do docker cp container:/$line .; done

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.