简体   繁体   English

使用 xargs 从本地系统复制文件到 docker 容器

[英]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 .我有一个包含大约 8000 张图像的文件夹,名称如下img_991_990_0_drawing.jpg.jpg I want to copy all files from my local system into a docker container.我想将本地系统中的所有文件复制到 docker 容器中。

OS: Ubuntu 18.04.2 LTS操作系统:Ubuntu 18.04.2 LTS

Docker: 18.09.2 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 .我尝试sudo docker cp <source_path> <container_id>:<dest_path>并遇到了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/dar.net/result_img/* | xargs /bin/cp 1f17d5044287:/home/dataturks/bazaar/uploads/2c91808274774ae60/所以我尝试find /data/projects/cad-detection/dar.net/result_img/* | xargs /bin/cp 1f17d5044287:/home/dataturks/bazaar/uploads/2c91808274774ae60/以下方式使用xargs复制所有文件find /data/projects/cad-detection/dar.net/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.我通常可以使用cp来复制我的文件,所以我过去从未使用过xargs 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.如果您想知道为什么我要复制一堆图像而不是一个文件夹,我正在使用 DataTurks,我需要/bazaar/uploads/2c91808274774ae60中的文件才能通过本地主机访问它们。

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 .首先,您在示例中使用的是/bin/cp ,而不是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... .其次,当你有一个看起来像xargs <cmd> <arg1>xargs命令时,这将产生<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).要获得docker cp的正确语法,您需要使用-i选项定义替换字符串,然后将其插入命令中的适当位置(因为目标目录需要是最后一个参数)。

The -print0 for find and the -0 for xargs are just there for the sake of correctness; find 的-print0xargs-0只是为了正确性而存在; they will allow things to work if you have filenames that contain whitespace.如果您的文件名包含空格,它们将允许工作。


As an alternative, consider just using tar instead:作为替代方案,考虑只使用tar代替:

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.当然,这需要在你的容器中安装tar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM