36

Similar to Copying files from host to Docker container, except docker cp doesn't seem to work for multiple files

$ docker cp data/a.txt sandbox_web_1:/usr/src/app/data/

works fine, but

$ docker cp data/*txt sandbox_web_1:/usr/src/app/data/

docker: "cp" requires 2 arguments.
See 'docker cp --help'.

Usage:  docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH

Copy files/folders between a container and the local filesystem
Use '-' as the source to read a tar archive from stdin
and extract it to a directory destination in a container.
Use '-' as the destination to stream a tar archive of a
container source to stdout.

Using docker 1.11.1 on Ubuntu 14.04x64

Community
  • 1
  • 1
kampta
  • 4,748
  • 5
  • 31
  • 51
  • 3
    Down-voters please mention the reason for down-voting, so that I can improve the question or take it off! – kampta May 11 '16 at 06:03
  • 1
    It is more a `bash` issue. A for loop would do the work: `for txt in $(ls data/*txt); do docker cp data/${txt} sandbox_web_1:/usr/src/app/data/; done` or maybe using the auto-completion of `oh-my-zsh`. – Auzias May 11 '16 at 09:08
  • This answer shows a more direct way, using tar: https://stackoverflow.com/a/28123384/3216427 It's probably more efficient for a lot of files, though I expect equivalent to the for loop for a small set. – joanis Jul 04 '19 at 15:27

5 Answers5

46

There is a proposal for docker cp to support wildcards (7710), but it is not implemented yet.

So that leaves you with bash scripting, using docker cp for each file:

for f in data/*txt; do docker cp $f sandbox_web_1:/usr/src/app/data/; done
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
21

The following command should copy whole directory data with its content of a data directory to your desired destination:

docker cp data/ sandbox_web_1:/usr/src/app/

Tested on Docker version 1.12.1, but I haven't found any changes to a cp command in a the release 1.12.1

Pavel Ducho
  • 311
  • 2
  • 3
  • 6
    That's not what is being asked. Only the content of the directory needs to be copied, not the directory itself. – Agustí Sánchez Oct 12 '17 at 23:49
  • this command copies the `data` folder, with it's containing files. use `docker cp data/* sandbox_web_1:/usr/src/app/` to copy all files in data folder to the destination – Reza Bayat Jul 27 '21 at 15:10
5

I am on Docker version 18.09 and found that I was able to copy all the files from my current local directory to the container's root by running: docker cp ./ image_name:

Aaron Bazzone
  • 311
  • 3
  • 3
3

Docker cp works perfectly fine when we are inside the directory whose contents we need to copy in bulk into the container. Apparently, the asterisk wildcard (*) is not supported for copying multiple files with docker cp command.

Copied the contents not the directory.

Also, given is the docker version for reference. Although, not much changes to the docker cp until lately from this post.

[root@stnm001 hadoop-configs]# docker version
Client:
 Version:      1.12.3
 API version:  1.24
 Go version:   go1.6.3
 Git commit:   6b644ec
 Built:
 OS/Arch:      linux/amd64

Server:
 Version:      1.12.3
 API version:  1.24
 Go version:   go1.6.3
 Git commit:   6b644ec
 Built:
 OS/Arch:      linux/amd64
[root@stnm001 hadoop-configs]#

[root@stnm001 hadoop-configs]# docker cp ./  "xyz-downloader:/etc/hadoop/conf"
[root@stnm001 hadoop-configs]#
[root@stnm001 hadoop-configs]# ls
capacity-scheduler.xml  container-executor.cfg  dfs.exclude    hadoop-metrics2.properties  hadoop-policy.xml  hdfs-site.xml     slaves          ssl-server.xml  yarn.exclude
configuration.xsl       core-site.xml           hadoop-env.sh  hadoop-metrics.properties   hbase-site.xml     log4j.properties  ssl-client.xml  yarn-env.sh     yarn-site.xml
[root@stnm001 hadoop-configs]#
[root@stnm001 hadoop-configs]#
[root@stnm001 hadoop-configs]# docker exec -it xyz-downloader bash
[root@xyz-downloader /]#
[root@xyz-downloader /]#
[root@xyz-downloader /]# cd /etc/hadoop/conf/
[root@xyz-downloader conf]# ls -ltrh
total 100K
-rw-r--r-- 1 root root  318 May 20 06:57 container-executor.cfg
-rw-r--r-- 1 root root 1.4K May 20 06:57 configuration.xsl
-rw-r--r-- 1 root root 3.6K May 20 06:57 capacity-scheduler.xml
-rw-r--r-- 1 root root 1.8K May 20 06:57 hadoop-metrics2.properties
-rw-r--r-- 1 root root 3.6K May 20 06:57 hadoop-env.sh
-rw-r--r-- 1 root root    0 May 20 06:57 dfs.exclude
-rw-r--r-- 1 root root 1.4K May 20 06:57 core-site.xml
-rw-r--r-- 1 root root 5.2K May 20 06:57 hdfs-site.xml
-rw-r--r-- 1 root root 9.1K May 20 06:57 hadoop-policy.xml
-rw-r--r-- 1 root root 2.5K May 20 06:57 hadoop-metrics.properties
-rw-r--r-- 1 root root  891 May 20 06:57 ssl-client.xml
-rw-r--r-- 1 root root  102 May 20 06:57 slaves
-rw-r--r-- 1 root root  15K May 20 06:57 log4j.properties
-rw-r--r-- 1 root root 4.7K May 20 06:57 yarn-env.sh
-rw-r--r-- 1 root root  891 May 20 06:57 ssl-server.xml
-rw-r--r-- 1 root root  11K May 20 06:57 yarn-site.xml
-rw-r--r-- 1 root root    0 May 20 06:57 yarn.exclude
-rw-r--r-- 1 root root 2.7K May 20 06:58 hbase-site.xml
[root@xyz-downloader conf]#
Sturdyone
  • 41
  • 2
3

Mixing @Aaron's answer with the fact that () gives a subshell in bash, I was able to accomplish this via:

(cd data && docker cp ./ sandbox_web_1:user/src/app/)

It takes advantage of the fact that ./ is almost the only parameter that can pass multiple files to docker cp.

Aidin
  • 25,146
  • 8
  • 76
  • 67