默认情况下 Docker 的守护进程启动会生成一个 socket (/var/run/docker.sock)进程通信文件,而并没有监听端口,只能在本机操作 Docker。如果想在其它地方操作 Docker 主机,就需要让 Docker 主机监听一个端口号,这样可以通过端口号就能实现远程操作。Docker 提供了 Python API Client,所以可以直接用Python直接和 Docker 守护进程通信。
一、在 Docker 主机上安装 docker-py
pip install docker-py
二、修改docker配置文件/etc/sysconfig/docker,添加以下参数,启用监听端口
other_args='--insecure-registry 10.44.86.222:5000 --graph=/data/docker/images --registry-mirror=https://10.44.86.222:5000 -H tcp://10.44.86.222:9527 -H unix:///var/run/docker.sock'
三、测试连接
[root@ali-sz01-qgz-prd-048 /root]#python
Python 2.7.8 (default, Feb 2 2016, 10:07:49)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import docker
>>> client = docker.Client(base_url='tcp://10.44.86.222:9527',version='1.21',timeout=5)
>>> print(client.images())
[{u'Created': 1454489329, u'Labels': {}, u'VirtualSize': 760166676, u'ParentId': u'4378e9fcb6924ee8b3a21c61dd7e7e2bb95b159b73d61cbc73059af108fe1efe', u'RepoTags': [u'tomcat-basess:latest'], u'RepoDigests': [], u'Id': u'2898c52a54b4a6fb0dd83ce1a18ae17e7504b0d1206540ba7529cedc363d51b9', u'Size': 0}, {u'Created': 1454489177, u'Labels': {}, u'VirtualSize': 760166676, u'ParentId': u'4378e9fcb6924ee8b3a21c61dd7e7e2bb95b159b73d61cbc73059af108fe1efe', u'RepoTags': [u'tomcat-bases:latest'], u'RepoDigests': [], u'Id': u'a0793529d2eb91e6eba33fcc2b5f6831252d30a3647a217a4088a0f0ae401491', u'Size': 0}, {u'Created': 1454488647, u'Labels': {}, u'VirtualSize': 760166676, u'ParentId': u'4378e9fcb6924ee8b3a21c61dd7e7e2bb95b159b73d61cbc73059af108fe1efe', u'RepoTags': [u'tomcat-base:latest'], u'RepoDigests': [], u'Id': u'c3040687f5b468c25247f497fadefce0ab208bbc5bb7743a68d5b94db0462ede', u'Size': 0}, {u'Created': 1454470181, u'Labels': None, u'VirtualSize': 381670700, u'ParentId': u'', u'RepoTags': [u'centos6-base:latest', u'10.44.86.222:5000/centos6-base:latest'], u'RepoDigests': [], u'Id': u'61520f109a13674ea1a0cfd8750e6b52552a4bcce6d3b30ef9ea549a451366c5', u'Size': 381670700}, {u'Created': 1454399313, u'Labels': None, u'VirtualSize': 381670724, u'ParentId': u'', u'RepoTags': [u'10.44.86.222:5000/centos6.7-base:latest'], u'RepoDigests': [], u'Id': u'0d58c43c95548fa41f7b64c3756f61616da129ee8cd94bc7ba8fe7a60f064f1b', u'Size': 381670724}, {u'Created': 1453844265, u'Labels': None, u'VirtualSize': 422820953, u'ParentId': u'4b0669413b8f2e10c5b06c5386713d2ccfde1b0edb05cd76504d7b9f46b0e775', u'RepoTags': [u'registry:latest'], u'RepoDigests': [], u'Id': u'07d93e41c370ee6d181a969117b4e0fefd9554659159ecc156e6d2f092ab6f44', u'Size': 0}]
>>>
四、版本对应错误解决方案
如果客户端版本与服务端版本对应不上,报以下错误,可以通过升级docker方式解决
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/python/lib/python2.7/site-packages/docker/api/container.py", line 68, in containers
res = self._result(self._get(u, params=params), True)
File "/usr/local/python/lib/python2.7/site-packages/docker/client.py", line 146, in _result
self._raise_for_status(response)
File "/usr/local/python/lib/python2.7/site-packages/docker/client.py", line 142, in _raise_for_status
raise errors.APIError(e, response, explanation=explanation)
docker.errors.APIError: 400 Client Error: Bad Request ("client is newer than server (client API version: 1.21, server API version: 1.19)")
升级方法:
1、下载docker最近稳定版
curl -sSL -O https://get.docker.com/builds/Linux/x86_64/docker-1.9.1
2、停止docker服务并备份文件
service docker stop
mv /usr/bin/docker /usr/bin/docker_bak
3、升级docker
mv docker-1.9.1 /usr/bin/docker
chmod +x /usr/bin/docker
service docker start
4、查看最新版本
docker -v
本文出自 “我的运维时光” 博客,请务必保留此出处http://aaronsa.blog.51cto.com/5157083/1741088