Solvedansible Failed to import docker-py for docker_container module
✔️Accepted Answer
docker-py is just the name of the project. It installs a python package named docker. So you should run:
# python
Python 2.7.6 (default, Oct 26 2016, 20:30:19)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import docker
>>> print(docker.version)
1.10.6
>>>
Downgrading docker-compose won't remove the docker package from the docker project which is installed on your server according to your pip output.
The following should fix your issue:
pip uninstall docker
pip uninstall docker-py
pip uninstall docker-compose
pip install docker-compose==1.9.0
Other Answers:
@jpiron
This works perfectly fine. Thanks!
But IMO this is just a workaround to get things keep running.
Nevertheless should this be fixed to avoid downgrading docker-compose.
Users keep running into this issue since one year. Why is this ticket closed?
@guenhter
This is due to your docker-compose version.
The docker-py package has been renamed into docker in version 2.0 (https://github.com/docker/docker-py/releases/tag/2.0.0). And in this version, Docker.Client has been renamed into docker.APIClient.
Docker-compose 1.10+ now requires docker instead of docker-py. And due to his name the docker package is before the docker-py one in the PYTHONPATH leading to the import error.
A workaround is to downgrade your docker-compose version to 1.9.0 the time the Ansible docker_container module updates its dependencies from docker-py to docker.
Why is this issue closed? We installed latest ansible 2.4.2.0 in latest RedHat 7.4, that is a clean machine from scratch. It requires docker-py and python3 is not installed. Ansible RPM provides docker.py, and NOT docker-py. It is inconsistent of Ansible to install docker.py and to use docker-py.py.
ISSUE TYPE
COMPONENT NAME
docker_container
ANSIBLE VERSION
CONFIGURATION
Nothing changed
OS / ENVIRONMENT
Linux machine1 3.16.0-4-amd64 #1 SMP Debian 3.16.36-1+deb8u1 (2016-09-03) x86_64 GNU/Linux
pip list:
python --version
Python 2.7.9
docker info
SUMMARY
When module
docker_container
is used, it fails every time with"Failed to import docker-py - cannot import name Client. Try `pip install docker-py`"
STEPS TO REPRODUCE
EXPECTED RESULTS
Image is executed.
ACTUAL RESULTS