dockerfiledocker-cp

Copy .txt files from docker container to host


So I have a docker container with .txt and .csv files in it. I need to copy these to host. But I only need to copy files .txt files. The command sudo docker cp -a env1_1:/path/*.txt . does not seem to work. Is copying files of a specific types possible using docker cp? I am unable to find any alternatives for now.

Any suggestions?

Thanks.


Solution

  • Indeed, docker cp does not support glob patterns, and as an aside, container paths are absolute:

    The docker cp command assumes container paths are relative to the container’s / (root) directory. This means supplying the initial forward slash is optional […]

    Local machine paths can be an absolute or relative value. The command interprets a local machine’s relative paths as relative to the current working directory where docker cp is run.

    However, one may devise a workaround, relying on docker exec, and a manually crafted shell command relying on the tar command on both sides (assuming it is available in the image):

    sudo docker exec env1_1 sh -c 'cd /path && tar cf - *.txt' | tar xvf -
    

    or if need be:

    sudo docker exec env1_1 sh -c 'cd /path && tar cf - *.txt' | ( cd /dest/path && tar xvf - )
    

    Here, the special filename - denotes STDOUT (or respectively STDIN).

    Usual disclaimer: the final tar command will overwrite without further notice the selected files in the current folder (or /dest/path in the second example).