adama
adama

Reputation: 557

Using xargs to copy files from local system into docker container

I have a folder with about 8000 images named like this img_991_990_0_drawing.jpg.jpg. I want to copy all files from my local system into a docker container.

OS: Ubuntu 18.04.2 LTS

Docker: 18.09.2

I tried sudo docker cp <source_path> <container_id>:<dest_path> and ran into sudo: unable to execute /usr/bin/docker: Argument list too long.

So I tried to copy all files with xargs the following way find /data/projects/cad-detection/darknet/result_img/* | xargs /bin/cp 1f17d5044287:/home/dataturks/bazaar/uploads/2c91808274774ae60/

But I will get the not a directory error

/bin/cp: target '/data/projects/cad-detection/darknet/result_img/img_2585_2581_0_drawing.jpg.jpg' is not a directory
/bin/cp: target '/data/projects/cad-detection/darknet/result_img/img_4076_4070_0_drawing.jpg.jpg' is not a directory
/bin/cp: target '/data/projects/cad-detection/darknet/result_img/img_5570_5560_0_drawing.jpg.jpg' is not a directory
/bin/cp: target '/data/projects/cad-detection/darknet/result_img/img_7052_7040_0_drawing.jpg.jpg' is not a directory
/bin/cp: target '/data/projects/cad-detection/darknet/result_img/img_999_998_0_drawing.jpg.jpg' is not a directory

I usually can use cp for copying my files, so I never used xargs in the past. I think I´m missing an option.

If you wonder why I want to copy a buch of images rather than one folder, I am working with DataTurks and I need the files in /bazaar/uploads/2c91808274774ae60 to have access to them via localhost.

Upvotes: 0

Views: 1077

Answers (1)

larsks
larsks

Reputation: 311740

I think you're looking for:

find /data/projects/cad-detection/darknet/result_img/ -type f -print0 |
xargs -0 -iIMG docker cp IMG 1f17d5044287:/home/dataturks/bazaar/uploads/2c91808274774ae60/

First, you were using /bin/cp in your example, rather than docker cp.

Secondly, when you have an xargs command that looks like xargs <cmd> <arg1>, this will produce commands of the form <cmd> <arg1> arg arg arg .... To get the correct syntax for docker cp, you need to use the -i option to define a replacement string, and then insert that in the appropriate place in the command (because the destination directory needs to be the last argument).

The -print0 for find and the -0 for xargs are just there for the sake of correctness; they will allow things to work if you have filenames that contain whitespace.


As an alternative, consider just using tar instead:

tar -C /data/projects/cad-detection/darknet/result_img/ -cf- . |
docker exec -i 1f17d5044287 tar -C /home/dataturks/bazaar/uploads/2c91808274774ae60/ -xf-

Of course, this requires that tar is installed in your container.

Upvotes: 1

Related Questions