Search code examples
pythondockerdockerpy

docker-py error when attempting to get a list of containers


I apologize if I am missing something obvious. I am trying to use docker-py version 5.0.0 with python 3.6 to connect to my local docker engine and run commands, but I receive an error when I attempt to access the list of running containers. I have received this same error on the docker:stable container and on a server running Ubuntu 18.04.

This is an example of how I get the error:

$ python3
>>> import docker
>>> print(docker.__version__)
5.0.0
>>> docker_client = docker.DockerClient(base_url='unix://var/run/docker.sock', version="1.41")
>>> docker_containers = docker_client.containers()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.6/site-packages/docker/models/resource.py", line 67, in __call__
    raise TypeError(
TypeError: 'ContainerCollection' object is not callable. You might be trying to use the old (pre-2.0) API - use docker.APIClient if so.

This is the versions of docker I am currently running:

$ docker version
Client: Docker Engine - Community
 Version:           20.10.8
 API version:       1.41
 Go version:        go1.16.6
 Git commit:        3967b7d
 Built:             Fri Jul 30 19:54:08 2021
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.8
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.16.6
  Git commit:       75249d8
  Built:            Fri Jul 30 19:52:16 2021
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.4.9
  GitCommit:        e25210fe30a0a703442421b0f60afac609f950a3
 runc:
  Version:          1.0.1
  GitCommit:        v1.0.1-0-g4144b63
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

As far as I can tell the latest version of docker engine API is 1.41 at this time.

What am I missing or doing wrong to cause this error?


Solution

  • After glancing once more at the docs, I am fairly sure it the below is correct.

    docker_client = docker.DockerClient(base_url='unix://var/run/docker.sock')
    docker_client.containers.list()
    

    https://docker-py.readthedocs.io/en/stable/containers.html#docker.models.containers.ContainerCollection.list