如何使用docker cp

时间:2018-07-10 12:19:17

标签: docker cp

我想使用通配符从容器中的目录中选择多个文件,并使用docker cp将这些文件从容器复制到docker主机。

我找不到docker cp是否支持使用通配符。

docker cp fd87af99b650:/foo/metrics.csv* /root/metrices_testing/

结果为错误metrics.csv*: no such file or directory

我遇到了一个示例,其中使用for loop选择一些文件然后发送到容器,但是我想将文件从容器传输到主机,并且想在脚本运行时在docker主机本身上执行此操作仅在主机上。

可以使用docker exec首先选择文件,然后使用docker cp复制文件。但这是一个两步过程。

有人可以帮我一步吗?

编辑:

我尝试过了。步骤已关闭,但仍然失败。

# for f in $(docker exec -it SPSRS bash -c "ls /opt/tpa/logs/metrics.csv*");
     do docker cp SPSRS:$f /root/metrices_testing/;
 done


: no such file or directory lstat /docker/overlay2/193d2ad0d8d087377e3b96cbfb672b0e39132ae5e961872127614c9396f8c068/merged/opt/SPS_18_5_R1/logs/metrics.csv.2018.07.10-08:45
: no such file or directory lstat /docker/overlay2/193d2ad0d8d087377e3b96cbfb672b0e39132ae5e961872127614c9396f8c068/merged/opt/SPS_18_5_R1/logs/metrics.csv.2018.07.10-09:00
: no such file or directory lstat /docker/overlay2/193d2ad0d8d087377e3b96cbfb672b0e39132ae5e961872127614c9396f8c068/merged/opt/SPS_18_5_R1/logs/metrics.csv.2018.07.10-09:15
: no such file or directory lstat /docker/overlay2/193d2ad0d8d087377e3b96cbfb672b0e39132ae5e961872127614c9396f8c068/merged/opt/SPS_18_5_R1/logs/metrics.csv.2018.07.10-09:30
: no such file or directory lstat /docker/overlay2/193d2ad0d8d087377e3b96cbfb672b0e39132ae5e961872127614c9396f8c068/merged/opt/SPS_18_5_R1/logs/metrics.csv.2018.07.10-09:45
: no such file or directory lstat /docker/overlay2/193d2ad0d8d087377e3b96cbfb672b0e39132ae5e961872127614c9396f8c068/merged/opt/SPS_18_5_R1/logs/metrics.csv.2018.07.10-10:00
: no such file or directory lstat /docker/overlay2/193d2ad0d8d087377e3b96cbfb672b0e39132ae5e961872127614c9396f8c068/merged/opt/SPS_18_5_R1/logs/metrics.csv.2018.07.10-10:15
: no such file or directory lstat /docker/overlay2/193d2ad0d8d087377e3b96cbfb672b0e39132ae5e961872127614c9396f8c068/merged/opt/SPS_18_5_R1/logs/metrics.csv.2018.07.10-10:30
: no such file or directory lstat /docker/overlay2/193d2ad0d8d087377e3b96cbfb672b0e39132ae5e961872127614c9396f8c068/merged/opt/SPS_18_5_R1/logs/metrics.csv.2018.07.10-10:45
: no such file or directory lstat /docker/overlay2/193d2ad0d8d087377e3b96cbfb672b0e39132ae5e961872127614c9396f8c068/merged/opt/SPS_18_5_R1/logs/metrics.csv.2018.07.10-11:00
: no such file or directory lstat /docker/overlay2/193d2ad0d8d087377e3b96cbfb672b0e39132ae5e961872127614c9396f8c068/merged/opt/SPS_18_5_R1/logs/metrics.csv.2018.07.10-11:15
: no such file or directory lstat /docker/overlay2/193d2ad0d8d087377e3b96cbfb672b0e39132ae5e961872127614c9396f8c068/merged/opt/SPS_18_5_R1/logs/metrics.csv.2018.07.10-11:30
: no such file or directory lstat /docker/overlay2/193d2ad0d8d087377e3b96cbfb672b0e39132ae5e961872127614c9396f8c068/merged/opt/SPS_18_5_R1/logs/metrics.csv.2018.07.10-11:45
: no such file or directory lstat /docker/overlay2/193d2ad0d8d087377e3b96cbfb672b0e39132ae5e961872127614c9396f8c068/merged/opt/SPS_18_5_R1/logs/metrics.csv.2018.07.10-12:00
: no such file or directory lstat /docker/overlay2/193d2ad0d8d087377e3b96cbfb672b0e39132ae5e961872127614c9396f8c068/merged/opt/SPS_18_5_R1/logs/metrics.csv.2018.07.10-12:15
: no such file or directory lstat /docker/overlay2/193d2ad0d8d087377e3b96cbfb672b0e39132ae5e961872127614c9396f8c068/merged/opt/SPS_18_5_R1/logs/metrics.csv.2018.07.10-12:30
: no such file or directory lstat /docker/overlay2/193d2ad0d8d087377e3b96cbfb672b0e39132ae5e961872127614c9396f8c068/merged/opt/SPS_18_5_R1/logs/metrics.csv.2018.07.10-12:45

5 个答案:

答案 0 :(得分:3)

Docker cp命令支持将文件夹中的所有内容复制到文件夹中

docker cp -a container-id:/opt/tpa/logs/  /root/testing/

在上面的示例中,将文件从容器文件夹/ opt / tpa / logs复制到本地计算机/ root / testing /文件夹。这里/ logs /中的所有文件都将被复制到本地。这里的技巧是使用 -a 选项和 docker cp

答案 1 :(得分:2)

实际上,您的解决方案可以使您的目标仅需进行一些更改:

for f in $(docker exec -it SPSRS bash -c "ls /opt/tpa/logs/metrics.csv*"); do docker cp SPSRS:$f /root/metrices_testing/; done

->

for f in $(docker exec -it SPSRS bash -c "ls /opt/tpa/logs/metrics.csv*"); do docker cp SPSRS:`echo $f | sed 's/\r//g'` /root/metrices_testing/; done

这是因为docker exec -it SPSRS bash -c "ls /opt/tpa/logs/metrics.csv*"在每个匹配的字符串中都会有\r,所以最终cp在容器中找不到文件。

因此,我们使用echo $f | sed 's/\r//g'来删除每个文件名的\r,这可以使您工作。

答案 2 :(得分:1)

Docker cp仍然不支持通配符。但是,您可以通过以下方式在Dockerfile中使用它们:

COPY hom* /mydir/        # adds all files starting with "hom"
COPY hom?.txt /mydir/    # ? is replaced with any single character, e.g., "home.txt"

参考:https://docs.docker.com/engine/reference/builder/#copy

答案 3 :(得分:0)

在容器中 中运行此:

dcp() {
  if [ "$#" -eq 1 ]; then
    printf "docker cp %q .\n" "$(hostname):$(readlink -e "$1")"
  else
    local archive="$(mktemp -t "export-XXXXX.tgz")"
    tar czf "$archive" "$@" --checkpoint=.52428800
    printf "docker exec %q cat %q | tar xvz -C .\n" "$(hostname)" "$archive"
  fi
}

然后选择要复制的文件:

dcp /foo/metrics.csv*

它将在容器内部创建一个档案,并吐出一个命令供您运行。在主机上运行该命令。

例如

docker exec 1c75ed99fa42 cat /tmp/export-x9hg6.tgz | tar xvz -C .

或者,我想您可以在没有临时存档的情况下做到这一点:

dcp() {
  if [ "$#" -eq 1 ]; then
    printf "docker cp %q .\n" "$(hostname):$(readlink -e "$1")"
  else
    printf "docker exec %q tar czC %q" "$(hostname)" "$PWD"
    printf " %q" "$@"
    printf " | tar xzvC .\n"
  fi
}

将为您生成一条命令,例如:

docker exec 1c75ed99fa42 tar czC /root .cache .zcompdump .zinit .zshrc .zshrc.d foo\ bar | tar xzvC .

那么您甚至不需要别名,这只是一个方便。

答案 4 :(得分:0)

predict(labels.name[0]) error: 18 names =[] 19 ---> 20 image = cv2.imread(image_path) 21 image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) 22 #image_yuv = cv2.cvtColor(image, cv2.COLOR_BGR2YUV) SystemError: <built-in function imread> returned NULL without setting an error 接受文件或tar存档,因此您可以将list of files provided as arguments打包到tar存档中,return the archive to stdout并通过管道传输到docker cp

docker cp