将 .txt 文件从 docker 容器复制到主机

Copy .txt files from docker container to host

所以我有一个 docker 容器,里面有 .txt 和 .csv 文件。我需要将这些复制到主机。但我只需要复制文件 .txt 文件。命令 sudo docker cp -a env1_1:/path/*.txt . 似乎不起作用。是否可以使用 docker cp 复制特定类型的文件?我暂时找不到任何替代品。

有什么建议吗?

谢谢。

的确,docker cp 不支持 glob 模式,另外,容器路径是 绝对:

The docker cp command assumes container paths are relative to the container’s / (root) directory. This means supplying the initial forward slash is optional […]

Local machine paths can be an absolute or relative value. The command interprets a local machine’s relative paths as relative to the current working directory where docker cp is run.

然而,人们可以设计一种解决方法,依赖于 docker exec,以及依赖于双方的 tar 命令的手动制作的 shell 命令(假设它在图片):

sudo docker exec env1_1 sh -c 'cd /path && tar cf - *.txt' | tar xvf -

或者如果需要:

sudo docker exec env1_1 sh -c 'cd /path && tar cf - *.txt' | ( cd /dest/path && tar xvf - )

这里,特殊文件名 - 表示 STDOUT(或分别为 STDIN)。

通常的免责声明:最后的 tar 命令将 覆盖,恕不另行通知 当前文件夹中的选定文件(或 /dest/path 在第二个示例中) .