You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ansible执行docker-compose任务时Docker Python SDK导入失败问题求助

Troubleshooting Ansible docker_compose Module "No module named 'docker'" Error

Let's break down the possible causes and actionable fixes for your issue, since you've already confirmed the Python interpreter path and installed the docker module:

1. User-Specific vs. System-Wide Module Installation Mismatch

It’s common to install Python modules with pip install --user docker, which places them in your personal ~/.local/lib directory. But if Ansible runs as the root user (the default unless you specify remote_user), it won’t access modules installed for a regular user.

Fix:
Install the docker module system-wide using the absolute path of your Python 3 interpreter to ensure it’s available to all users:

sudo /usr/bin/pip3 install docker

Since your Python version is 3.6.9 (which is no longer supported by the latest docker SDK), install a compatible version:

sudo /usr/bin/pip3 install docker==4.4.4

(Versions of the Docker SDK for Python >=5.0 require Python 3.7+, so this older version works with 3.6.)

2. Confirm Ansible is Using the Correct Python Interpreter

Even if you set ansible_python_interpreter in ansible.cfg, individual inventory entries or playbook-level variables might override this setting. Let’s verify the interpreter Ansible actually uses on the managed node:

Add this debug task before your docker_compose task:

- name: Check Python interpreter details
  debug:
    msg: "Using Python path: {{ ansible_python.executable }}, Version: {{ ansible_python.version.full }}"

If the output doesn’t show /usr/bin/python3 and version 3.6.9, explicitly set the interpreter for the docker_compose task:

- name: Run docker-compose
  docker_compose:
    project_src: "{{ my_project_path }}"
    files:
      - docker-compose.yaml
      - other_file.yaml
  vars:
    ansible_python_interpreter: /usr/bin/python3

3. Validate Module Accessibility Directly on the Managed Node

Log into your managed host and run this command to check if the docker module is accessible to /usr/bin/python3:

/usr/bin/python3 -c "import docker; print('Module found at:', docker.__file__)"
  • If this fails: The module isn’t installed for this Python version. Reinstall using /usr/bin/pip3 as mentioned earlier.
  • If this succeeds: The issue lies with Ansible’s execution environment. Check if Ansible runs with a different user or missing environment variables. You can explicitly set the PYTHONPATH in your task to include the module’s directory:
    - name: Run docker-compose with correct PYTHONPATH
      docker_compose:
        project_src: "{{ my_project_path }}"
        files:
          - docker-compose.yaml
          - other_file.yaml
      environment:
        PYTHONPATH: "/usr/local/lib/python3.6/dist-packages:/home/your_user/.local/lib/python3.6/site-packages"
    
    Replace /home/your_user with the actual username if you installed modules locally.

4. Rule Out Virtual Environment Conflicts

If your managed node uses virtual environments (like virtualenv or pyenv), ensure Ansible isn’t accidentally picking up a different Python environment. Using the absolute path /usr/bin/python3 should avoid this, but double-check there’s no alias or environment variable overriding it.


内容的提问来源于stack exchange,提问作者Max

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 10:07:33