'Failed to import docker or docker-py - No module named docker

I've installed Docker and Ansible to my AWS Ec2 Linux as follow:

sudo yum update -y
sudo yum install docker -v
sudo service docker start

sudo yum-config-manager --enable epel
sudo yum repolist
sudo yum install ansible

I've found following error message when I've tried to pull docker images to my AWS Ec2 Linux with ansible.

fatal: [127.0.0.1]: FAILED! => {"changed": false, "msg": "Failed to import docker or docker-py - No module named docker. Try `pip install docker` or `pip install docker-py` (Python 2.6)"}

Docker version

Client:
 Version:           18.06.1-ce
 API version:       1.38
 Go version:        go1.10.3
 Git commit:        e68fc7a215d7133c34aa18e3b72b4a21fd0c6136
 Built:             Fri Oct 26 23:38:19 2018
 OS/Arch:           linux/amd64
 Experimental:      false

Ansible version is

ansible 2.6.8
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/home/ec2-user/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.6/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.6.9 (unknown, Nov  2 2017, 19:21:21) [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)]

Here is my part of ansible playbook file

- name: Pull a container image
  docker_container:
    name: mynodejs
    image: registry.gitlab.com/ppshein/test:latest
    pull: yes
    state: started
    published_ports:
      - 8080:80

Please let me know which I'm missed to configure inside AWS Ec2 Linux.



Solution 1:[1]

What worked for me for ansible 2.9.1 and Ubuntu 20.10 was installing python3-docker:

sudo apt-get install python3-docker

Solution 2:[2]

Add this ansible_python_interpreter on to your 'hosts' file:

Change [servers:vars] to [your-group-of-server-names:vars]

For Python >= 2.7

[servers:vars]
ansible_python_interpreter=/usr/bin/python3 # For Python3 [default Ubuntu-18.04]

Python <= 2.7

[servers:vars]
ansible_python_interpreter=/usr/bin/python # For Python2.7

Solution 3:[3]

For people passing by, had the same issue with ubuntu installed via apt giving 2.7.15 on Ubuntu. Fixed by installing latest version with:

sudo apt install software-properties-common
sudo apt-add-repository --yes --update ppa:ansible/ansible
sudo apt install ansible

that gives 2.9.2 as of today.

Solution 4:[4]

I had this same issue. I was getting the error:

fatal: [localhost]: FAILED! => {"changed": false, "msg": "Failed to import docker or docker-py - No module named docker.

I was using the following:

Ubuntu 20.04
Ansible 2.10.7
Python 3.8.10
pip 20.0.2 (pip3)

Here's how I fixed mine:

So first I ran the command to remove all existing copies of docker, docker-py and docker-compose python libraries:

pip3 uninstall docker docker-py docker-compose

And then ran the command below to install the python docker-compose library alongside the python docker library

pip3 install docker-compose

Note: Please do not add sudo to the command

That's all.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Bogdan Iordache
Solution 2 Shaze
Solution 3 dev93
Solution 4 Dharman