docker-py icon indicating copy to clipboard operation
docker-py copied to clipboard

`client.containers.run()` doesn't exist

Open Funky185540 opened this issue 5 years ago • 2 comments

Hi there,

Today I decided to look into docker-py for a project I will be working on in the coming months. After a fresh install of docker and docker-py today, I must say that, somehow, docker-py doesn't quite work. The getting started examples show this code:

>>> import docker
>>> client = docker.from_env()
>>> client.containers.run("ubuntu", "echo hello world")
'hello world\n'

But when I run this, I see

In [1]: import docker                                                                                                                                                                                                                         
In [2]: client = docker.from_env()                                                                                                                                                                                                            
In [3]: client.containers.run("ubuntu", "echo hello world")                                                                                                                                                                                                         
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-3-5336699d83c5> in <module>
----> 1 client.containers.run()

AttributeError: 'function' object has no attribute 'run'

I also tried to manually create an instance of the docker.Client as detailed in the docs

In [1]: import docker                                                                                                                                                                                                                         
# Note: The following line from the docs fails to execute... :-(
In [2]: client = docker.DockerClient(base_url='unix://var/run/docker.sock')                                                                                                                                                                   
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-2-a0131c4bce50> in <module>
----> 1 client = docker.DockerClient(base_url='unix://var/run/docker.sock')

AttributeError: module 'docker' has no attribute 'DockerClient'

In [3]: client = docker.Client(base_url='unix://var/run/docker.sock')                                                                                                                                                                         
In [4]: client.containers.run("hello-world")                                                                                                                                                                                                  
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-4-a00eac6bdc71> in <module>
----> 1 client.containers.run("hello-world")

AttributeError: 'function' object has no attribute 'run'

Calling docker through the CLI works as expected

$ docker run hello-world

Hello from Docker!
This message shows that your installation appears to be working correctly.
[ ... ]

Note that I am a member of the docker group on my system. I already tried to reboot my system but to no avail.

Here's some info on my system:

$ docker --version
Docker version 19.03.11, build 42e35e61f3

$ pip3 show docker
Name: docker
Version: 4.2.1
Summary: A Python library for the Docker Engine API.
Home-page: https://github.com/docker/docker-py
Author: None
Author-email: None
License: Apache License 2.0
Location: /home/<user>/.local/lib/python3.7/site-packages
Requires: websocket-client, six, requests
Required-by: 

$ cat /etc/os-release 
PRETTY_NAME="Debian GNU/Linux 10 (buster)"
NAME="Debian GNU/Linux"
VERSION_ID="10"
VERSION="10 (buster)"
VERSION_CODENAME=buster

I'd be grateful for any help and/or pointers to a possible cause...

Funky185540 avatar Jun 19 '20 15:06 Funky185540

Edit: I don't have the same issue.

cron410 avatar Jun 22 '20 13:06 cron410

You probably installed docker-py from PyPi which is an old version from 2016. Remove it and install docker instead from PyPi.

Both modules are imported as import docker but the old one doesn't have the run() method.

I just ran against this issue myself.

DylanVanAssche avatar Oct 25 '20 07:10 DylanVanAssche