# manage docker via Ansible - question on the python docker library/package on the target

**URL:** <https://forum.ansible.com/t/manage-docker-via-ansible-question-on-the-python-docker-library-package-on-the-target/36813>\
**Category:** Ansible Project\
**Created:** [July 7, 2022, 9:16am UTC](https://forum.ansible.com/t/manage-docker-via-ansible-question-on-the-python-docker-library-package-on-the-target/36813 "2022-07-07T09:16:07Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dulhaver](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulhaver/32/1061_2.png) [@dulhaver](https://forum.ansible.com/u/dulhaver)\
**Post date:** [July 7, 2022, 9:16am UTC](https://forum.ansible.com/t/manage-docker-via-ansible-question-on-the-python-docker-library-package-on-the-target/36813/1 "2022-07-07T09:16:07Z")

</div>

hi everybody,

I try to handle docker containers via Ansible and am getting this error:

msg: **'Failed to import the required Python library (Docker SDK for Python:** docker (Python \>= 2.7) or docker-py (Python 2.6)) on vm-51150-0191’‘s Python /usr/libexec/platform-python. Please read module documentation and install in the appropriate location. If the required library is installed, but Ansible is using the wrong Python interpreter, please consult the documentation on ansible\_python\_interpreter, for example via `pip install docker` or `pip install docker-py` (Python 2.6). The error was: No module named ‘‘docker’’’

I assume that means the target host needs to have a python docker module/library/package (sorry I am not 100% aware what the correcet term is here).  
I read **pip install docker** to be the common solution, but am advised in my workplace to go around pip if possible (I miss the knowledge to discuss the reasons for this though).

So, is the alternative to install it manually (I found these instructions)?

1. Download the package
2. unzip it if it is zipped
3. _cd_ into the directory containing setup.py
4. If there are any installation instructions contained in documentation, read and follow the instructions OTHERWISE
5. type in _python setup.py install_  
and would this be the package I need: [https://github.com/docker/\*\*docker-py\*\*/releases](https://github.com/docker/ **docker-py** /releases) ?

---

<div class="post-metadata">

**Author:** ![felixfontein](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/felixfontein/32/7530_2.png) [@felixfontein](https://forum.ansible.com/u/felixfontein)\
**Post date:** [July 7, 2022, 11:59am UTC](https://forum.ansible.com/t/manage-docker-via-ansible-question-on-the-python-docker-library-package-on-the-target/36813/2 "2022-07-07T11:59:28Z")

</div>

Hi,

> I try to handle docker containers via Ansible and am getting this  
> error:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: 'Failed to import the required Python library (Docker SDK  
> for Python: docker (Python \>= 2.7) or docker-py (Python 2.6)) on  
> vm-51150-0191''s Python /usr/libexec/platform-python. Please read  
> module documentation and install in the appropriate location. If the  
> required library is installed, but Ansible is using the wrong Python  
> interpreter, please consult the documentation on  
> ansible\_python\_interpreter, for example via `pip install docker` or  
> `pip install docker-py` (Python 2.6). The error was: No module named  
> ''docker'''
> 
> I assume that means the target host needs to have a python docker  
> module/library/package (sorry I am not 100% aware what the correcet  
> term is here).

yes, that is correct. Generally the requirements for a module are  
needed in the environment the module is executed. Most modules run on  
the target host. (If you don't use `delegate_to: localhost` or  
something like that, or have one of the rare modules which are actually  
implemented as action plugins and thus run on the controller.)

> I read pip install docker to be the common solution, but am advised  
> in my workplace to go around pip if possible (I miss the knowledge to  
> discuss the reasons for this though).
> 
> So, is the alternative to install it manually (I found these  
> instructions)?

The best alternative is usually to install it from system packages. For  
example, on Ubuntu and Debian, there's the python3-docker system  
package you can install. For other OSes similar packages might exist.

Depending on which modules/plugins you need, you might also be  
interested in the current 3.0.0-a1 prerelease of community.docker,  
where some of the modules were rewritten to work with only requests  
(and some more dependencies, depending on your Python version, and how  
exactly you connect to the Docker daemon; see  
[https://github.com/ansible-collections/community.docker#external-requirements](https://github.com/ansible-collections/community.docker#external-requirements)  
for more details). For a list of plugins/modules that were adjusted so  
far see  
[https://github.com/ansible-collections/community.docker/issues/364#issuecomment-1172957628](https://github.com/ansible-collections/community.docker/issues/364#issuecomment-1172957628)  
Please note that these new versions haven't yet been extensively  
tested, so there might be bugs.

Best regards,  
Felix

---

<div class="post-metadata">

**Author:** ![dulhaver](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulhaver/32/1061_2.png) [@dulhaver](https://forum.ansible.com/u/dulhaver)\
**Post date:** [July 7, 2022, 12:39pm UTC](https://forum.ansible.com/t/manage-docker-via-ansible-question-on-the-python-docker-library-package-on-the-target/36813/3 "2022-07-07T12:39:51Z")

</div>

> The best alternative is usually to install it from system packages. For  
> example, on Ubuntu and Debian, there's the python3-docker system  
> package you can install. For other OSes similar packages might exist.

same same (python3-docker) on this rhel8 host. Will go for that then. thx for the wisdom on this

---

<div class="post-metadata">

**Author:** ![Nico\_Kadel-Garcia](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/nico_kadel-garcia/32/9998_2.png) [@Nico\_Kadel-Garcia](https://forum.ansible.com/u/Nico_Kadel-Garcia)\
**Post date:** [July 7, 2022, 11:19pm UTC](https://forum.ansible.com/t/manage-docker-via-ansible-question-on-the-python-docker-library-package-on-the-target/36813/4 "2022-07-07T23:19:07Z")

</div>

> Hi,
> 
> \> I try to handle docker containers via Ansible and am getting this  
> \> error:  
> \> msg: 'Failed to import the required Python library (Docker SDK  
> \> for Python: docker (Python \>= 2.7) or docker-py (Python 2.6)) on  
> \> vm-51150-0191''s Python /usr/libexec/platform-python. Please read  
> \> module documentation and install in the appropriate location. If the  
> \> required library is installed, but Ansible is using the wrong Python  
> \> interpreter, please consult the documentation on  
> \> ansible\_python\_interpreter, for example via `pip install docker` or  
> \> `pip install docker-py` (Python 2.6). The error was: No module named  
> \> ''docker'''  
> \>  
> \> I assume that means the target host needs to have a python docker  
> \> module/library/package (sorry I am not 100% aware what the correcet  
> \> term is here).
> 
> yes, that is correct. Generally the requirements for a module are  
> needed in the environment the module is executed. Most modules run on  
> the target host. (If you don't use `delegate_to: localhost` or  
> something like that, or have one of the rare modules which are actually  
> implemented as action plugins and thus run on the controller.)

Sounds like you've got a different ansible-core, and a different  
python, than wherever you manually installed this module. Don't do  
that, it's very confusing to do manually for each module this way.

I'm guessing that you used "/usr/bin/python" on an older OS, such as  
RHEL 7, for which it is python 2.7. Don't! Use python3. For example,  
if you need to use a more modern ansible-core:

I'm also guessing that you want the "python-docker" module, and not  
the "docker" python module. Yes, it's really foolish to put the word  
"python" in the name of a python module, I didn't do it!

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pip3 install --user ansible-core  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pip3 install --user python-docker

Always double check python modules with the word "python" in their  
name, because RHEL is inconsistent about leaving in or stripping that  
word "python" in the RPM name.

Don't bother with the plain "pip" or python", it just gets confusing,  
always use pip3 or python3, unless you want your brain to hurt with  
just this sort of confusion.

Also, don't bother with the full "ansible" package. IIt's confusing,  
and it does not contain ansible. The packaging has become somewhat  
byzantine. Install "ansible-core", and use that consistently, since it  
contains all the basic ansible executable and python modules anyway.

> \> I read pip install docker to be the common solution, but am advised  
> \> in my workplace to go around pip if possible (I miss the knowledge to  
> \> discuss the reasons for this though).

The name of the module you're looking for is "python-docker". And yes,  
it's confusing as heck, why for goddess's sake is there an extra  
python in the name? You may as well ask why ansible elected to split  
into "ansible-core", that actually contains ansible, and "ansible"  
that has at least 300 MBytes of modules mostly used by so very few  
people, and just leaft them alone as the also individually published  
versions?

> \> So, is the alternative to install it manually (I found these  
> \> instructions)?
> 
> The best alternative is usually to install it from system packages. For  
> example, on Ubuntu and Debian, there's the python3-docker system  
> package you can install. For other OSes similar packages might exist.

On RHEL 7, which is what I'm guessing you're using due to the  
out-of-date but still system default python 2.7, There isn't a  
published RPM for ansible-core on RHEL 7, and \*I'm\* not going to do  
one, ansible-core

For RHEL 7, \*I\* publish tools to build ansible-core RPMs, but in your  
shoes I'd jump to RHEL 8 and python 3.8 and get a contemporary,  
supported version of ansible-core. My tools are at  
[https://github.com/nkadel/ansiblerepo/](https://github.com/nkadel/ansiblerepo/) and you're welcome to play with  
them, but I'd suggest you save some time hop to an RPM supported  
operating system.

> Depending on which modules/plugins you need, you might also be  
> interested in the current 3.0.0-a1 prerelease of community.docker,

If I may suggest? If you're not aware of the distinct versions of  
python, then playing with bleeding edge version of docker is likely to  
waste quitea a lot of your time climbing a steep learning curve, much  
as you're encountering with ansible's confusing and inconsistent  
support of python 2 and python 3. I'd instead invest the time in  
getting off any base operating system which still uses python 2.7 as  
its default.

---

<div class="post-metadata">

**Author:** ![Nico\_Kadel-Garcia](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/nico_kadel-garcia/32/9998_2.png) [@Nico\_Kadel-Garcia](https://forum.ansible.com/u/Nico_Kadel-Garcia)\
**Post date:** [July 7, 2022, 11:22pm UTC](https://forum.ansible.com/t/manage-docker-via-ansible-question-on-the-python-docker-library-package-on-the-target/36813/5 "2022-07-07T23:22:39Z")

</div>

If that's what he needs. "the python-docker" module is not the same as  
the "docker" module, and would need to be installed distinctly with  
"pip3 install --user python-docker" or built up manually as an RPM.  
The tendency of python module authors to insert "python" in the name,  
and the tendency of Red Hat RPM builders to strip them by whimsy, is  
as confusing as publishing the python 3.8 based version of  
"ansiblc-core" as "ansible-core" instead of as  
"python38-ansible-core". It's one of Red Hat's more confusing  
practices.

---

<div class="post-metadata">

**Author:** ![felixfontein](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/felixfontein/32/7530_2.png) [@felixfontein](https://forum.ansible.com/u/felixfontein)\
**Post date:** [July 8, 2022, 5:33am UTC](https://forum.ansible.com/t/manage-docker-via-ansible-question-on-the-python-docker-library-package-on-the-target/36813/6 "2022-07-08T05:33:34Z")

</div>

Hi,

> \> \> The best alternative is usually to install it from system  
> \> \> packages. For example, on Ubuntu and Debian, there's the  
> \> \> python3-docker system package you can install. For other OSes  
> \> \> similar packages might exist.  
> \>  
> \> same same (python3-docker) on this rhel8 host. Will go for that  
> \> then. thx for the wisdom on this
> 
> If that's what he needs. "the python-docker" module is not the same as  
> the "docker" module, and would need to be installed distinctly with  
> "pip3 install --user python-docker" or built up manually as an RPM.  
> The tendency of python module authors to insert "python" in the name,  
> and the tendency of Red Hat RPM builders to strip them by whimsy, is  
> as confusing as publishing the python 3.8 based version of

the Python library is called "docker"  
([https://pypi.org/project/docker/](https://pypi.org/project/docker/)), and the DEB and RPM packages are  
called "python3-docker".

Cheers,  
Felix

---

<div class="post-metadata">

**Author:** ![Nico\_Kadel-Garcia](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/nico_kadel-garcia/32/9998_2.png) [@Nico\_Kadel-Garcia](https://forum.ansible.com/u/Nico_Kadel-Garcia)\
**Post date:** [July 8, 2022, 1:08pm UTC](https://forum.ansible.com/t/manage-docker-via-ansible-question-on-the-python-docker-library-package-on-the-target/36813/7 "2022-07-08T13:08:11Z")

</div>

I agree with your reasoning, and hope it's borne out by the original  
poster. Unfortunately, based on the python 2.7 references, I'm  
assuming he's on RHEL 7. or a similar OS. Those don't have a publicly  
published python3-docker package. Unless you see one that I don't?
