简体   繁体   English

python docker sdk 如何在containers.run 中运行多个命令

[英]python docker sdk how to run multiple commands in containers.run

I am using python 3 with docker sdk and using containers.run in order to create a container and run my code when I use command argument with one command as a string it works fine我将 python 3 与 docker sdk 一起使用,并使用containers.run来创建容器并运行我的代码,当我将命令参数与一个命令用作字符串时,它工作正常

see code看代码

client = docker.from_env()
container = client.containers.run(image=image, command="echo 1")

When I try to use a list of commands (which is fine according to the docs)当我尝试使用命令列表时(根据文档这很好)

client = docker.from_env()
container = client.containers.run(image=image, command=["echo 1", "echo 2"])

I am getting this error我收到此错误

OCI runtime create failed: container_linux.go:345: starting container process caused "exec: \\"echo 1\\": executable file not found in $PATH OCI 运行时创建失败:container_linux.go:345:启动容器进程导致“exec:\\”echo 1\\”:在 $PATH 中找不到可执行文件

same happens when using one string as such使用一个字符串时也会发生同样的情况

"echo 1; echo 2"

I am using ubuntu 19 with docker我正在使用 ubuntu 19 和 docker

Docker version 18.09.9, build 1752eb3 Docker 版本 18.09.9,构建 1752eb3

It used to work just fine with a list of commands, is there anything wrong with the new version of docker or am i missing something here?它曾经可以很好地处理命令列表,新版本的 docker 有什么问题,还是我在这里遗漏了什么?

尝试这个:

container = client.containers.run(image="alpine:latest", command=["/bin/sh", "-c", 'echo 1 && echo 2'])

You can use this.你可以用这个。

client = docker.from_env()
container = client.containers.run(image=image, command='/bin/sh')
result = container.exec_run('echo 1')
result = container.exec_run('echo 2')
   
container.stop()
container.remove()

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

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