3

I want to transfer multiple files from ubuntu to a docker container. For single file, the below command works:

docker cp file_name CONTAINER:path/

But I am not able to upload multiple files at once. I have tried following commands, but no success yet:

docker cp {file1,file2} CONTAINER:path/
docker cp [file1,file2] CONTAINER:path/
docker cp ["file1","file2"] CONTAINER:path/

All above commands returns "no such file or directory" error

Gaurav
  • 1,214
  • 2
  • 17
  • 32
  • 2
    in the doc https://docs.docker.com/engine/reference/commandline/cp/ they say `SRC_PATH specifies a file ` or `SRC_PATH specifies a directory ` Maybe put all your files in a folder and copy the folder? – user2915097 Sep 15 '16 at 09:19
  • Folder move is working fine. But uploading whole folder is not required every time. Is there any command where I can upload multiple files selectively? – Gaurav Sep 15 '16 at 09:27
  • to the best of my knowledge, no. At the moment, `docker cp` lacks some improvements – user2915097 Sep 15 '16 at 09:38
  • see also http://stackoverflow.com/questions/29939419/copying-file-from-host-to-container?rq=1 – user2915097 Sep 15 '16 at 09:45
  • and https://github.com/WhisperingChaos/dkrcp – user2915097 Sep 15 '16 at 09:46
  • you can use some with netcat like `one trick I used recently to avoid restarting the container was netcat "nc" in the container: nc -l 10101 > thefile.bin on the host: cat thefile.bin | nc 172.0.2.3 10101 (where 172.0.2.3 is the container ip, and 10101 is a random port of your choosing)` from https://github.com/docker/docker/issues/905 – user2915097 Sep 15 '16 at 12:50

1 Answers1

4

You can use this:

$ tar Ccf $(dirname SRC_PATH) - $(basename SRC_PATH) | docker exec -i foo tar Cxf DEST_PATH -

Using - as the SRC_PATH streams the contents of STDIN as a tar archive. The command extracts the content of the tar to the DEST_PATH in container’s filesystem. In this case, DEST_PATH must specify a directory. Using - as the DEST_PATH streams the contents of the resource as a tar archive to STDOUT.

You can read more here.

Farhad Farahi
  • 35,528
  • 7
  • 73
  • 70