Python 使用带有docker py的管道
在的示例中,它们将命令的结果返回到docker图像,如下所示:Python 使用带有docker py的管道,python,docker,dockerpy,Python,Docker,Dockerpy,在的示例中,它们将命令的结果返回到docker图像,如下所示: >>> client.containers.run("ubuntu:latest", "echo hello world") 'hello world\n' 我想要的是使用管道——例如,如果我能做到这一点,那就太好了: >>> client.containers.run("ubuntu:latest", "echo hello world | wc") ' 1 2
>>> client.containers.run("ubuntu:latest", "echo hello world")
'hello world\n'
我想要的是使用管道——例如,如果我能做到这一点,那就太好了:
>>> client.containers.run("ubuntu:latest", "echo hello world | wc")
' 1 2 12\n'
然而,我得到的却是:
>>> client.containers.run("ubuntu:latest", "echo hello world | wc")
b'hello world | wc\n'
在docker中运行两个命令(第二个命令从第一个命令通过管道传输)的最简单方法是什么?无论何时使用shell构造,如
$ENV_VAR
,
等,请确保您确实有一个shell来解释它们,否则它们将有其文本值!要了解调用缺少shell的原因,您必须了解dockerENTRYPOINT
和CMD
如果您查看dockerfile for,您将看到它是
FROM scratch
并且该文件没有设置入口点
,只有CMD
。阅读一些关于差异的好看的信息。只需说明一下,在您的情况下,图像名称之后的所有内容都将替换cmd
containers.run()
的文档说明命令可以是str
或列表
。从这一点,以及观察到的行为,我们可以推断命令字符串将被拆分为空白,以创建docker exec的参数列表
简而言之,答案是因为|
是一个shell构造,但您没有执行shell。有几种方法可以将壳添加到等式中。最明显的是直接运行shell:
>>> client.containers.run("ubuntu:latest", "bash -c 'echo hello world | wc'",)
' 1 2 12\n'
但是您也可以将entrypoint设置为shell,这通常在通用容器中完成(不过请注意,您仍然必须确保提供了-c
,并且必须像以前一样引用整个shell命令。entrypoint只提供可执行文件,而不提供任何参数)
命令行对标准输入字段分隔符执行相同的操作:
$ docker run --rm -it ubuntu:latest echo hello world \| wc
hello world | wc
$ docker run --rm -it ubuntu:latest "echo hello world \| wc"
docker: Error response from daemon: OCI runtime create failed: container_linux.go:345: starting container process caused "exec: \"echo hello world \\\\| wc\": executable file not found in $PATH": unknown.
如果我们引用了整件事,我们就无法实现输入字段分隔符周围的自动拆分:
$ docker run --rm -it ubuntu:latest echo hello world \| wc
hello world | wc
$ docker run --rm -it ubuntu:latest "echo hello world \| wc"
docker: Error response from daemon: OCI runtime create failed: container_linux.go:345: starting container process caused "exec: \"echo hello world \\\\| wc\": executable file not found in $PATH": unknown.
python的等价物是:
>>> client.containers.run("ubuntu:latest",["echo hello world \\|"])
Traceback (most recent call last):
[... traceback omitted for brevity ...]
docker.errors.APIError: 500 Server Error: Internal Server Error ("OCI runtime create failed: container_linux.go:345: starting container process caused "exec: \"echo hello world \\\\|\": executable file not found in $PATH": unknown")
无论何时使用shell构造,如$ENV_VAR
,
等,请确保您实际有一个shell来解释它们,否则它们将有其文本值!要了解调用缺少shell的原因,您必须了解dockerENTRYPOINT
和CMD
如果您查看dockerfile for,您将看到它是
FROM scratch
并且该文件没有设置入口点
,只有CMD
。阅读一些关于差异的好看的信息。只需说明一下,在您的情况下,图像名称之后的所有内容都将替换cmd
containers.run()
的文档说明命令可以是str
或列表
。从这一点,以及观察到的行为,我们可以推断命令字符串将被拆分为空白,以创建docker exec的参数列表
简而言之,答案是因为|
是一个shell构造,但您没有执行shell。有几种方法可以将壳添加到等式中。最明显的是直接运行shell:
>>> client.containers.run("ubuntu:latest", "bash -c 'echo hello world | wc'",)
' 1 2 12\n'
但是您也可以将entrypoint设置为shell,这通常在通用容器中完成(不过请注意,您仍然必须确保提供了-c
,并且必须像以前一样引用整个shell命令。entrypoint只提供可执行文件,而不提供任何参数)
命令行对标准输入字段分隔符执行相同的操作:
$ docker run --rm -it ubuntu:latest echo hello world \| wc
hello world | wc
$ docker run --rm -it ubuntu:latest "echo hello world \| wc"
docker: Error response from daemon: OCI runtime create failed: container_linux.go:345: starting container process caused "exec: \"echo hello world \\\\| wc\": executable file not found in $PATH": unknown.
如果我们引用了整件事,我们就无法实现输入字段分隔符周围的自动拆分:
$ docker run --rm -it ubuntu:latest echo hello world \| wc
hello world | wc
$ docker run --rm -it ubuntu:latest "echo hello world \| wc"
docker: Error response from daemon: OCI runtime create failed: container_linux.go:345: starting container process caused "exec: \"echo hello world \\\\| wc\": executable file not found in $PATH": unknown.
python的等价物是:
>>> client.containers.run("ubuntu:latest",["echo hello world \\|"])
Traceback (most recent call last):
[... traceback omitted for brevity ...]
docker.errors.APIError: 500 Server Error: Internal Server Error ("OCI runtime create failed: container_linux.go:345: starting container process caused "exec: \"echo hello world \\\\|\": executable file not found in $PATH": unknown")
这很简单:
client.containers.run("ubuntu:latest", "sh -c 'echo hello world | wc'")
这很简单:
client.containers.run("ubuntu:latest", "sh -c 'echo hello world | wc'")
谢谢,这太完美了!谢谢,这太完美了!谢谢你的解释!谢谢你的解释!