繁体   English   中英

通过 docker-py 使用管道

[英]Using pipes with docker-py

https://github.com/docker/docker-py的示例中,它们将命令的结果返回到 docker 映像,如下所示:

>>> client.containers.run("ubuntu:latest", "echo hello world")
'hello world\n'

我想要的是使用 pipe - 例如,如果我能这样做会很棒:

>>> 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 来解释它们,否则它们将具有它们的字面值,要了解您的调用为什么缺少外壳,您必须了解 docker ENTRYPOINTCMD

如果您查看ubuntu:latest的 dockerfile,您会发现它是

FROM scratch

而且该文件没有设置ENTRYPOINT ,只有CMD 阅读CMD 和 Dockerfile 中的 ENTRYPOINT 有什么区别? 有关差异的一些好看的信息。 可以说,在您的情况下,图像名称之后的所有内容都替换了cmd

containers.run()的文档说command可以是strlist 由此,根据观察到的行为,我们可以推断命令字符串将在空白处拆分,以创建 arguments 列表,用于 docker exec。

所以,简而言之,答案是因为| 是 shell 构造,但您没有执行 shell。 有几种方法可以将 shell 添加到等式中。 最明显的是直接运行shell:

>>> client.containers.run("ubuntu:latest", "bash -c 'echo hello world | wc'",)
'      1       2      12\n'

但是您也可以将入口点设置为 shell,这通常在通用容器中完成(但请注意,您仍然必须确保提供-c ,并且必须像以前一样引用整个 shell 命令。入口点仅提供可执行文件,而不是任何参数)。

>>> client.containers.run("ubuntu:latest", "-c 'echo hello world | wc'", entrypoint="bash")
'      1       2      12\n'

命令行使用标准输入字段分隔符执行相同的操作:

$ 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'")

暂无
暂无

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

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