ImportError: No module named 'requests.packages.urllib3' 解决方法
https://www.xiaomastack.com/2015/04/17/docker-py/
docker-py是访问docker api的一个python模块,该模块可以在python脚本中很方便的调用docker api,如果要自己造这个轮子的话可以参考docker api的说明文档
https://docs.docker.com/reference/api/docker_remote_api_v1.16/
安装docker-py,该项目的github地址是
https://github.com/docker/docker-py
cd /opt/repos/pip-packages/
$ tar zxf docker-py-1.10.6.tar.gz $ cd docker-py-1.10.6 $ python setup.py build $ python setup.py install
使用方法可以参考其提供的文档http://docker-py.readthedocs.org/en/latest/ 没有什么特别的,比如:
>>> from docker import Client
>>> c = Client(base_url='unix://var/run/docker.sock')
>>> c.version()
{u'KernelVersion': u'2.6.32-504.el6.x86_64', u'Arch': u'amd64', u'ApiVersion': u'1.16', u'Version': u'1.4.1', u'GitCommit': u'5bc2ff8/1.4.1', u'Os': u'linux', u'GoVersion': u'go1.3.3'}
#可以使用sock访问,也可以使用TCP访问(默认TCP方式没有打开,需要在docker服务启动的时候指定)
>>> c = Client(base_url='http://127.0.0.1:2345')
>>> c.version()
{u'KernelVersion': u'2.6.32-504.el6.x86_64', u'Arch': u'amd64', u'ApiVersion': u'1.16', u'Version': u'1.4.1', u'GitCommit': u'5bc2ff8/1.4.1', u'Os': u'linux', u'GoVersion': u'go1.3.3'}
>>>
ImportError: No module named 'requests.packages.urllib3'
下载requests-2.20.1-py2.py3-none-any.whl requests-2.20.1.tar.gz这两个包,放到/opt/repos/pip-packages目录下
mv requests-2.20.1-py2.py3-none-any.whl /opt/repos/pip-packages mv requests-2.20.1.tar.gz /opt/repos/pip-packages pip install --upgrade --force-reinstall 'requests==2.20.1' urllib3
使用过程中出现了点小问题,情景还原如下:
>>> from docker import Client
>>> c = Client(base_url='unix://var/run/docker.sock')
>>> c.version()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.6/site-packages/docker_py-1.1.0-py2.6.egg/docker/client.py", line 944, in version
return self._result(self._get(url), json=True)
File "/usr/lib/python2.6/site-packages/docker_py-1.1.0-py2.6.egg/docker/client.py", line 124, in _result
self._raise_for_status(response)
File "/usr/lib/python2.6/site-packages/docker_py-1.1.0-py2.6.egg/docker/client.py", line 120, in _raise_for_status
raise errors.APIError(e, response, explanation=explanation)
docker.errors.APIError: 404 Client Error: Not Found ("client and server don't have same version (client : 1.17, server: 1.16)")
>>>
由于默认支持的docker api版本是1.17,而我目前使用的docker api是1.16,很简单修改/usr/lib/python2.6/site-packages/docker_py-1.1.0-py2.6.egg/docker/client.py文件的相关常量即可:
#DEFAULT_DOCKER_API_VERSION = '1.17'
DEFAULT_DOCKER_API_VERSION = '1.16'
不过如果docker api的版本低于1.15时docker-py-1.1.0不支持,需要下载docker-py较低的版本。