Search code examples
dockerdockerpy

Getting list of running docker containers using docker-py


I'm using docker-py to manage docker containers for one of my apps. I want to retrieve the list of all running containers, identical to docker ps. But the containers method only returns an empty list.

>>> import docker
>>> c = docker.Client()
>>> container = c.create_container(image='base', command='/bin/bash', stdin_open=True, tty=True, name='docker-test')
>>> container
{u'Id': u'1998f08a031d8632400264af667d93162465a04348b61144a4c730c7c4462875', u'Warnings': None}
>>> c.start(container)
>>> c.containers(quiet=False, all=True, trunc=True, latest=False, since=None, before=None, limit=-1)
[]

But, of course, doing $ docker ps gives me this:

CONTAINER ID        IMAGE                   COMMAND             CREATED             STATUS              PORTS               NAMES
1998f08a031d        base:latest             /bin/bash           3 minutes ago       Up 3 minutes                            docker-test

What am I missing out here?


Solution

  • Alright, I figured out what was wrong. The client and servers didn't have the same versions. When I re-initialize docker.Client with the correct version number, it returns a list of all the containers running.