Docker-py: Container not starting after creation

Created on 29 Aug 2014  ·  7Comments  ·  Source: docker/docker-py

Ubuntu: 14.04 (x64)
docker.io: 0.9.1~dfsg1-2 (
docker-py: 0.4.0 (installed via pip)

I'm trying to start a container with docker-py, but c.start(…) only returns None. This is the test-script I've been using:

import docker
from pprint import pprint

c = docker.Client(base_url='unix://var/run/docker.sock',
          version="1.10",
          timeout=20)
c.version()
print("Images:")
print("======")
pprint(c.images())
my_container = c.create_container('ubuntu', '/bin/bash')


print("my_container:")
print("============")
pprint(my_container)

print("------------------------")
print("trying to start %s" % (my_container['Id'], ))
erg = c.start(my_container['Id'])
print("erg: %s" % (erg, ))

And this is my output. Am I missing something?

Most helpful comment

You have to set tty=True, stdin_open=True, parameters when create a new container.

All 7 comments

What kind of output were you expecting? Looking at the source, the Client.start method doesn't refturn anything. So you should be fine if no exception is being raised. Alternatively you could do:

meta = c.inspect_container(my_container['Id'])
if meta['State']['Running']:
    print('{0} is running'.format(my_container['Id']))

Oh, must have overlooked that. Sorry. But the container isn't running nevertheless. 'docker ps' shows no containers.

Hm, I think that's because the command /bin/bash exits immediately. You should be able to see if and when it exited by running docker ps -a

Thanks for the tip, @groundeffect. The container is indeed exiting with "0" right after the start. But shouldn't /bin/bash keep it running?

breuer@crusher ~> sudo docker ps -a
CONTAINER ID        IMAGE                          COMMAND                CREATED             STATUS              PORTS                    NAMES
a43761ad5f82        ubuntu:14.04                   /bin/bash              2 minutes ago       Exit 0                                       condescending_wright

You have to set tty=True, stdin_open=True, parameters when create a new container.

@ColinHuang Thanks! That was missing. Isn't this something worth mentioning in the README? Perhaps in a "I'm new here section"?

I am using tty=True and stdin_open=True ...

import docker
client = docker.from_env()
rcappdb = client.containers.run('rcapplication_database:latest', command='rcapplication_database_create /database new.db', tty=True, stdin_open=True, detach=True)
rcappdb.exec_run(['rcapplication_database_devserver', '/database', 'new.db']) # is the container not running here for at least 3 seconds?
(some Traceback)
APIError: 500 Server Error: Internal Server Error ("Container 9707dbaed711af4fee65981416e22773d5f281d76666e32ca8fbdc6b3aeecc37 is not running")

... but I have still the problem that the container goes into exit after 3 seconds?

❯ docker ps -a
CONTAINER ID        IMAGE                            COMMAND                  CREATED             STATUS                         PORTS               NAMES
9707dbaed711        rcapplication_database:latest    "rcapplication_dat..."   4 seconds ago       Exited (0) 1 second ago                            brave_davinci
Was this page helpful?
0 / 5 - 0 ratings