docker copy all files in directory with given extention

Viewed 1265

I can copy one file from a docker container to the server with

docker cp docker_session_name:/root/mydir/ .

I would like know to copy only files from mydir with a given extension, say, pdf

2 Answers

I don't think you can do this with docker cp command

To do this you can mount the directory inside the docker and then you can run the regular cp command with regex to copy it to another directory.

Mount:

docker run -d --name containerName -v myvol2:/app imageName:tag

Inside Container:

cp app/*.pdf /destination

it looks like you cant just run like in Linux (see similar thread)

Not like this:

docker cp docker_session_name:/root/mydir/*.pdf .

simple answer

use this script:

path="/root/mydir"
for file in $(docker exec docker_session_name sh -c "ls ${path}/*.pdf"); do
        docker cp docker_session_name:${file} .
done

credits to this thread

cumbersome answer with easier use (no script)

you could however mount a bind mount between the host and the wanted path like so in the docker run command:

docker run -v /host/path/:/root/mydir/ my-image

then run cp with wildcard *.pdf from the host path of /host/path/ used in the docker run command

Related