Answers: <p>In order to use the Docker Ansible modules, you need the Python docker module installed.</p>
  <p>See <a href="https://docs.ansible.com/ansible/latest/collections/community/docker/docsite/scenario_guide.html#requirements" rel="nofollow noreferrer">the Ansible Docker requirements</a>:</p>
  <blockquote>
  <p>Most of the modules and plugins in community.docker require the Docker SDK for Python. The SDK needs to be installed on the machines where the modules and plugins are executed, and for the Python version(s) with which the modules and plugins are executed.</p>
  </blockquote>
  <p>So you need to do a <code>pip install docker</code> (assuming python3) before using the <code>community.docker</code> collection.</p>
  
Answers: CC BY-SA 4.0
Answers: 2022-03-28T01:14:00.840Z
Answers: After activating my virtualenv, I ran pip install --force docker, and the docker module was re-installed. I then tried running my playbook but got the same importerror.  I also tried running python interpreter and importing docker and it worked fine.  Is there something else I can do to get this to work?
Answers: dnraikes
Answers: CC BY-SA 4.0
Answers: 2022-03-28T17:58:22.537Z
Answers: I started fresh with a new compute instance as my ansible manager.  I used pip3 to install ansible, docker, and oci, and verified that both the oci and docker modules could be loaded in the python interactive interpreter.  I then reran my playbook and still get the same error.  python 3.6.8, docker module 5.0.3  ansible 2.11.9.
Answers: dnraikes
Answers: CC BY-SA 4.0
Answers: 2022-03-27T11:04:56.250Z
Answers: 2022-03-27T11:04:56.250Z
Answers: Bruce Becker
Answers: <p>This fixed my issue,</p>
  <pre><code> ansible-galaxy collection install community.docker --force
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2024-10-16T13:56:35.117Z
Answers: 2024-10-16T13:56:35.117Z
Answers: Rafaf Tahsin
Body: <p>I am trying to write a playbook that will download a docker image and run it on a group of servers.
  I have a python 3.6 virtualenv created and installed ansible into it.
  I then ran:</p>
  <pre><code>ansible-galaxy collections install community.docker
  </code></pre>
  <p>I verified that the collection was downloaded and is in my
  <code>.ansible/collections/ansible_collections folder.</code></p>
  <p>My playbook looks like:</p>
  <pre class="lang-yaml prettyprint-override"><code>    - name: test docker
        hosts: all
        collections:
          - community.docker
        become: true
      
        tasks:
          - name: log into the ocir registry
            docker_login:
              registry_url: &quot;{{ ocir_registry }}&quot;
              username: &quot;{{ docker_user }}&quot;
              password: &quot;{{ ocir_auth_token }}&quot;
              state: present
      
          - name: ensure a container is running
            docker_container:
              name: LDAPServer
              state: started
              image: &quot;{{ docker_image }}&quot;
              pull: true
              restart_policy: always
              ports:
                - &quot;3000:3000&quot;
  </code></pre>
  <p>When I run this playbook with:</p>
  <pre><code>    ansible-playbook docker.yml
  </code></pre>
  <p>I keep getting the following error:</p>
  <pre><code> 
  &quot;/tmp/ansible_community.docker.docker_login_payload_KMQkW7/ansible_community.docker.docker_login_p 
          ayload.zip/ansible_collections/community/docker/plugins/module_utils/common.py&quot;, line 33, 
          in &lt;module&gt;
          from docker import __version__ as docker_version
      ImportError: No module named docker
  </code></pre>
  <p>I just double-checked and I did install docker in my virtualenv, and I have the virtualenv activated when running the playbook.</p>
  <p>What am I doing wrong, or haven't I done to set things up correctly?</p>
  
Tags: <docker><ansible>
Title: Why can't my playbook find the community.docker modules
UserDisplayName: dnraikes
ThreadText: # Why can't my playbook find the community.docker modules
  
  (Asked by: username_0 on 2022-03-27)
  
  I am trying to write a playbook that will download a docker image and run it on a group of servers.
  I have a python 3.6 virtualenv created and installed ansible into it.
  I then ran:
  
  ```
  ansible-galaxy collections install community.docker
  
  ```
  
  I verified that the collection was downloaded and is in my
  `.ansible/collections/ansible_collections folder.`
  
  My playbook looks like:
  
  ```yaml
      - name: test docker
        hosts: all
        collections:
          - community.docker
        become: true
      
        tasks:
          - name: log into the ocir registry
            docker_login:
              registry_url: "{{ ocir_registry }}"
              username: "{{ docker_user }}"
              password: "{{ ocir_auth_token }}"
              state: present
      
          - name: ensure a container is running
            docker_container:
              name: LDAPServer
              state: started
              image: "{{ docker_image }}"
              pull: true
              restart_policy: always
              ports:
                - "3000:3000"
  
  ```
  
  When I run this playbook with:
  
  ```
      ansible-playbook docker.yml
  
  ```
  
  I keep getting the following error:
  
  ```
   
  "/tmp/ansible_community.docker.docker_login_payload_KMQkW7/ansible_community.docker.docker_login_p 
          ayload.zip/ansible_collections/community/docker/plugins/module_utils/common.py", line 33, 
          in <module>
          from docker import __version__ as docker_version
      ImportError: No module named docker
  
  ```
  
  I just double-checked and I did install docker in my virtualenv, and I have the virtualenv activated when running the playbook.
  
  What am I doing wrong, or haven't I done to set things up correctly?
  
  **Tags:** docker, ansible
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2022-03-27. Score: 2
  
  In order to use the Docker Ansible modules, you need the Python docker module installed.
  
  See [the Ansible Docker requirements](https://docs.ansible.com/ansible/latest/collections/community/docker/docsite/scenario_guide.html#requirements):
  
  > Most of the modules and plugins in community.docker require the Docker SDK for Python. The SDK needs to be installed on the machines where the modules and plugins are executed, and for the Python version(s) with which the modules and plugins are executed.
  
  So you need to do a `pip install docker` (assuming python3) before using the `community.docker` collection.
  
  > **Comments:**
  > **username_0**: After activating my virtualenv, I ran pip install --force docker, and the docker module was re-installed. I then tried running my playbook but got the same importerror.  I also tried running python interpreter and importing docker and it worked fine.  Is there something else I can do to get this to work?  
  > **username_0**: I started fresh with a new compute instance as my ansible manager.  I used pip3 to install ansible, docker, and oci, and verified that both the oci and docker modules could be loaded in the python interactive interpreter.  I then reran my playbook and still get the same error.  python 3.6.8, docker module 5.0.3  ansible 2.11.9.  
  
  ---
  
  ### Answer by username_2 on 2024-10-16. Score: 1
  
  This fixed my issue,
  
  ```
   ansible-galaxy collection install community.docker --force
  
  ```
  
  
  ---
  
  