2

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

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

same happens when using one string as such

"echo 1; echo 2"

I am using ubuntu 19 with docker

Docker version 18.09.9, build 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?

thebeancounter
  • 4,261
  • 8
  • 61
  • 109
  • the command accepts string or list, you can pass both eighter string or list. with the string you can try `client.containers.run("alpine:latest", command='sh -c "echo 1 && echo 2"')` https://docker-py.readthedocs.io/en/stable/containers.html – Adiii Nov 25 '19 at 11:50

2 Answers2

6

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()
  • Welcome to SO! You could improve the quality of your answer with a few explanations. – Timus Oct 30 '20 at 20:59
  • This only works when the image doesn't close automatically. If it does, then this answer will help you: https://stackoverflow.com/a/54623344/9698467 – Stefan Zhelyazkov Mar 14 '23 at 18:21
4

try this:

container = client.containers.run(image="alpine:latest", command=["/bin/sh", "-c", 'echo 1 && echo 2'])
LinPy
  • 16,987
  • 4
  • 43
  • 57
  • I need a method of performing a list of commands, this was a simple example of the problem, but I need to be able to use a longer list and more complex commands than echo... The API states that this should be possible – thebeancounter Nov 26 '19 at 10:08
  • Can you post a full example for using bin bash with who commands? – thebeancounter Nov 26 '19 at 10:11
  • `whoami` is in the path in alpine image so `container = client.containers.run(image="alpine:latest", command=['whoami'])` will work – LinPy Nov 26 '19 at 10:14
  • working with whoami but not working with echo... I need to be able to run more advanced scripts than whoami unfortunately – thebeancounter Nov 26 '19 at 10:19
  • Then you need to create a script and set it as you command in your image. As you normally do in pure docker setup – LinPy Nov 26 '19 at 10:21
  • What am i missing here? the docs clearly offer an option of using a list of commands – thebeancounter Nov 27 '19 at 20:17