kampta
kampta

Reputation: 4898

Copy multiple local files to docker container

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

Upvotes: 37

Views: 39861

Answers (6)

Ishrak
Ishrak

Reputation: 547

A pipeline of commands can also be used to copy multiple files to a docker container. The following pipeline works for me:

ls *txt | xargs -I{} docker cp {} <container_id>:<dest_inside_container>

Upvotes: 0

Aidin
Aidin

Reputation: 30017

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.

Upvotes: 3

Sturdyone
Sturdyone

Reputation: 41

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]#

Upvotes: 3

Aaron Bazzone
Aaron Bazzone

Reputation: 311

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:

Upvotes: 5

Pavel Ducho
Pavel Ducho

Reputation: 311

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

Upvotes: 21

VonC
VonC

Reputation: 1325137

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

Upvotes: 49

Related Questions