使用python的docker-py实现docker的api操作
前沿:
? ? ? ? 聽同事說,以后的dba申請可能有部分走其他部門的docker ,那邊貌似在搞一個類似docker的平臺管理系統(tǒng),據(jù)說很霸道。于是乎,我自己也想嘗試寫一個簡單的doker管理平臺。 ?做為起步我先搞搞docker api,docker官網(wǎng)有個docker-py,用起來很是清爽簡單。?
有關docker的更多的文章,大家可以到我的獨立博客看看。 ? ?xiaorui.cc
首先安裝docker的python相關的模塊。
| 1 2 3 4 5 6 7 8 9 10 | root@dev-ops:~#?pip?install?docker-py Requirement?already?satisfied?(use?--upgrade?to?upgrade):?docker-py?in?/usr/local/lib/python2.7/dist-packages Requirement?already?satisfied?(use?--upgrade?to?upgrade):?requests==2.2.1?in?/usr/lib/python2.7/dist-packages?(from?docker-py) Requirement?already?satisfied?(use?--upgrade?to?upgrade):?six>=1.3.0?in?/usr/lib/python2.7/dist-packages?(from?docker-py) Requirement?already?satisfied?(use?--upgrade?to?upgrade):?websocket-client==0.11.0?in?/usr/local/lib/python2.7/dist-packages?(from?docker-py) Requirement?already?satisfied?(use?--upgrade?to?upgrade):?mock==1.0.1?in?/usr/local/lib/python2.7/dist-packages?(from?docker-py) Requirement?already?satisfied?(use?--upgrade?to?upgrade):?coverage==3.7.1?in?/usr/local/lib/python2.7/dist-packages?(from?docker-py) Cleaning?up... root@dev-ops:~#? root@dev-ops:~# |
咱們先查看下已經(jīng)下載好的模塊
[root@dev-ops ~]$docker images
| 1 2 3 4 5 6 7 8 9 10 | http://rfyiamcool.blog.51cto.com/1030776/1539500 REPOSITORY???????????????TAG?????????????????IMAGE?ID????????????CREATED?????????????VIRTUAL?SIZE ubuntu_redis?????????????latest??????????????57672d1b5979????????About?an?hour?ago???257.6?MB rastasheep/ubuntu-sshd???14.04???????????????636d86f228c7????????2?days?ago??????????254.9?MB rastasheep/ubuntu-sshd???12.04???????????????1147568d245b????????6?days?ago??????????144.6?MB rastasheep/ubuntu-sshd???13.10???????????????8a63145eed52????????6?days?ago??????????231.6?MB rastasheep/ubuntu-sshd???latest??????????????47386e322f5d????????8?days?ago??????????254.9?MB lemonbar/centos6-ssh?????latest??????????????b78c71c001db????????11?days?ago?????????296.9?MB rastasheep/ubuntu-sshd???12.10???????????????c9c48c3cb34c????????4?weeks?ago?????????188.9?MB rastasheep/ubuntu-sshd???13.04???????????????bf49ae3d5534????????4?weeks?ago?????????213.1?MB |
查看已經(jīng)啟動的容器
[root@dev-ops ~]$docker ps -a
| 1 2 3 | CONTAINER?ID????????IMAGE???????????????????????????COMMAND?????????????CREATED?????????????STATUS??????????????PORTS???????????????????NAMES c5f6d0240531????????ubuntu_redis:latest?????????????/usr/sbin/sshd?-D???About?an?hour?ago???Up?About?an?hour????0.0.0.0:49154->22/tcp???redis_sshd?????????? df7b7568fa7a????????rastasheep/ubuntu-sshd:latest???/usr/sbin/sshd?-D???About?an?hour?ago???Up?About?an?hour????0.0.0.0:49153->22/tcp???test_sshd |
??
查看 docker 相關的版本
原文:?http://rfyiamcool.blog.51cto.com/1030776/1539500
[root@dev-ops ~]$docker version
| 1 2 3 4 5 6 7 8 | Client?version:?0.9.1 Go?version?(client):?go1.2.1 Git?commit?(client):?3600720 Server?version:?0.9.1 Git?commit?(server):?3600720 Go?version?(server):?go1.2.1 Last?stable?version:?1.1.2,?please?update?docker [root@dev-ops?~]$ |
好了,到現(xiàn)在為止 docker image有了,容器也有了,docker-py這個api也有了。。。
下面是我的操作的記錄,有過shell操作經(jīng)驗的人,應該就很好上手的。
[root@dev-ops ~]$ipython
Python 2.7.6 (default, Mar 22 2014, 22:59:56)?
Type "copyright", "credits" or "license" for more information.
IPython 1.2.1 -- An enhanced Interactive Python.
? ? ? ? ? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help ? ? ?-> Python's own help system.
object? ? -> Details about 'object', use 'object??' for extra details.
In [1]: import docker
創(chuàng)建docker的鏈接,這里是通過sock連接的。
In [2]: c = docker.Client(base_url='unix://var/run/docker.sock',version='1.9',timeout=10)
我們可以看到里面有很多的功能。
In [3]: c.
c.adapters ? ? ? ? ? ? ? ? ? ? ?c.copy ? ? ? ? ? ? ? ? ? ? ? ? ?c.headers ? ? ? ? ? ? ? ? ? ? ? c.login ? ? ? ? ? ? ? ? ? ? ? ? c.proxies ? ? ? ? ? ? ? ? ? ? ? c.send
c.attach ? ? ? ? ? ? ? ? ? ? ? ?c.create_container ? ? ? ? ? ? ?c.history ? ? ? ? ? ? ? ? ? ? ? c.logs ? ? ? ? ? ? ? ? ? ? ? ? ?c.pull ? ? ? ? ? ? ? ? ? ? ? ? ?c.start
c.attach_socket ? ? ? ? ? ? ? ? c.create_container_from_config ?c.hooks ? ? ? ? ? ? ? ? ? ? ? ? c.max_redirects ? ? ? ? ? ? ? ? c.push ? ? ? ? ? ? ? ? ? ? ? ? ?c.stop
c.auth ? ? ? ? ? ? ? ? ? ? ? ? ?c.delete ? ? ? ? ? ? ? ? ? ? ? ?c.images ? ? ? ? ? ? ? ? ? ? ? ?c.mount ? ? ? ? ? ? ? ? ? ? ? ? c.put ? ? ? ? ? ? ? ? ? ? ? ? ? c.stream
c.base_url ? ? ? ? ? ? ? ? ? ? ?c.diff ? ? ? ? ? ? ? ? ? ? ? ? ?c.import_image ? ? ? ? ? ? ? ? ?c.options ? ? ? ? ? ? ? ? ? ? ? c.remove_container ? ? ? ? ? ? ?c.tag
c.build ? ? ? ? ? ? ? ? ? ? ? ? c.events ? ? ? ? ? ? ? ? ? ? ? ?c.info ? ? ? ? ? ? ? ? ? ? ? ? ?c.params ? ? ? ? ? ? ? ? ? ? ? ?c.remove_image ? ? ? ? ? ? ? ? ?c.top
c.cert ? ? ? ? ? ? ? ? ? ? ? ? ?c.export ? ? ? ? ? ? ? ? ? ? ? ?c.insert ? ? ? ? ? ? ? ? ? ? ? ?c.patch ? ? ? ? ? ? ? ? ? ? ? ? c.request ? ? ? ? ? ? ? ? ? ? ? c.trust_env
c.close ? ? ? ? ? ? ? ? ? ? ? ? c.get ? ? ? ? ? ? ? ? ? ? ? ? ? c.inspect_container ? ? ? ? ? ? c.ping ? ? ? ? ? ? ? ? ? ? ? ? ?c.resize ? ? ? ? ? ? ? ? ? ? ? ?c.verify
c.commit ? ? ? ? ? ? ? ? ? ? ? ?c.get_adapter ? ? ? ? ? ? ? ? ? c.inspect_image ? ? ? ? ? ? ? ? c.port ? ? ? ? ? ? ? ? ? ? ? ? ?c.resolve_redirects ? ? ? ? ? ? c.version
c.containers ? ? ? ? ? ? ? ? ? ?c.get_image ? ? ? ? ? ? ? ? ? ? c.kill ? ? ? ? ? ? ? ? ? ? ? ? ?c.post ? ? ? ? ? ? ? ? ? ? ? ? ?c.restart ? ? ? ? ? ? ? ? ? ? ? c.wait
c.cookies ? ? ? ? ? ? ? ? ? ? ? c.head ? ? ? ? ? ? ? ? ? ? ? ? ?c.load_image ? ? ? ? ? ? ? ? ? ?c.prepare_request ? ? ? ? ? ? ? c.search ? ? ? ? ? ? ? ? ? ? ? ?
這里是查看已經(jīng)部署好的images鏡像
In [3]: c.images()
Out[3]:?
[{u'Created': 1407902496,
? u'Id': u'57672d1b597912c7b6841c05c0e07e525890cfd64cbd37c84754e7d7e54ee766',
? u'ParentId': u'47386e322f5deffbc04c777632e1f1ac7e6ddd6909ae6b9ce8d1d78dc2bb6d0c',
? u'RepoTags': [u'ubuntu_redis:latest'],
? u'Size': 2675587,
? u'VirtualSize': 257554797},
?{u'Created': 1407684299,
? u'Id': u'636d86f228c7c6e844d1473245a9580d9c57fd0bfd1b5b9172aa5c168581978c',
? u'ParentId': u'e882dd610d1ca64817106dcbda29b2c78536f5fb75f780583cc9fac0efe05398',
? u'RepoTags': [u'rastasheep/ubuntu-sshd:14.04'],
? u'Size': 0,
? u'VirtualSize': 254925704},
?{u'Created': 1407342004,
? u'Id': u'1147568d245b369495b19b1943c0946a2b2d823d0a96b48bb7bee5f128090d29',
? u'ParentId': u'12175cbcbe499c80dd71e3085467d9178a1b5cfa47ecf1463302e09fe2f09e58',
? u'RepoTags': [u'rastasheep/ubuntu-sshd:12.04'],
? u'Size': 0,
? u'VirtualSize': 144645266},
?{u'Created': 1407341844,
? u'Id': u'8a63145eed52cf9ae6bdb064f370469d6fa3653f0da5d370ece9e270baf8d9cb',
? u'ParentId': u'ca60f67629d2c43012fb53f714e60672c4ad5b61520bb306e3a35614190dcf76',
? u'RepoTags': [u'rastasheep/ubuntu-sshd:13.10'],
? u'Size': 0,
? u'VirtualSize': 231639963},
?{u'Created': 1407167259,
? u'Id': u'47386e322f5deffbc04c777632e1f1ac7e6ddd6909ae6b9ce8d1d78dc2bb6d0c',
? u'ParentId': u'a8bbc54283bbc341c703970ed429e4def6c034273ede59aaf451449164c95775',
? u'RepoTags': [u'rastasheep/ubuntu-sshd:latest'],
? u'Size': 0,
? u'VirtualSize': 254879210},
?{u'Created': 1406884906,
? u'Id': u'b78c71c001db87eb2da7049f5f9610039afd3484f65e22a3284098792460040d',
? u'ParentId': u'e13b39edfcb4c5e15f876c2824fea1d738fb3825437162fad497138123f3235b',
? u'RepoTags': [u'lemonbar/centos6-ssh:latest'],
? u'Size': 0,
? u'VirtualSize': 296869021},
?{u'Created': 1405333140,
? u'Id': u'c9c48c3cb34c4b8305d7bae4f0c3c753fbca3ac96195e73e9fdab4a8fa24708a',
? u'ParentId': u'30ffc511bbe11f6205cdc0f2749cadcc0c2eca4f7296285b1af867afcb6ef842',
? u'RepoTags': [u'rastasheep/ubuntu-sshd:12.10'],
? u'Size': 0,
? u'VirtualSize': 188947523},
?{u'Created': 1405333120,
? u'Id': u'bf49ae3d55344648b354bddf22b2f15c48e2d74e52933404757a9eb470ac35e3',
? u'ParentId': u'561b2d67fd0a80367e9ab98f9893ce6d36006dbb452e7c8693f028fcaa5f6c7f',
? u'RepoTags': [u'rastasheep/ubuntu-sshd:13.04'],
? u'Size': 0,
? u'VirtualSize': 213124480}]
這里是搜索鏡像,比如我想搜下, ubuntu redis 字眼的,結(jié)果沒有搜到。 ?但是搜ubuntu_sshd有關的鏡像會發(fā)現(xiàn)有的。
原文:?http://rfyiamcool.blog.51cto.com/1030776/1539500
In [5]: c.search('ubuntu_redis')
Out[5]: []
In [6]: c.search('ubuntu_sshd')
Out[6]:?
[{u'description': u'',
? u'is_official': False,
? u'is_trusted': False,
? u'name': u'pungoyal/ubuntu_sshd',
? u'star_count': 0}]
這里是下載鏡像
In []: c.pull('core9/docker-mongodb')
In [6]: c.create_container(image="ubuntu_redis", command="/usr/sbin/sshd -D",name="test_nima")
Out[6]:?
{u'Id': u'15bd8d4ecd1a50604732e1f81763eb347a1cf8937da929799e65261b004894ce',
?u'Warnings': None}
In [7]:?
啟動容器
In [7]: c.start(container="15bd8d4ecd1a50604732e1f81763eb347a1cf8937da929799e65261b004894ce")
查看日志
In [8]: c.logs(container="15bd8d4ecd1a50604732e1f81763eb347a1cf8937da929799e65261b004894ce")
Out[8]: ''
我們再試圖關閉下這個容器,然后inspect看下他的運行狀態(tài)
In [18]: c.stop('15bd8d4ecd1a')
In [20]: re_info = c.inspect_container('15bd8d4ecd1a')
In [22]: re_info['State']['Running']
Out[31]: False
原文:?http://rfyiamcool.blog.51cto.com/1030776/1539500
好了,我們通過docker shell看下剛才創(chuàng)建的結(jié)果。 我們在用python docker api的時候,創(chuàng)建了一個叫test_nima的容器。 ?
篇幅的問題就不多說了, api幾乎包含了docker的功能,最少能滿足我的應用,及在開發(fā)中與運維平臺的對接。朋友的公司已經(jīng)在用純node.js寫了套api,為公司內(nèi)部服務。其實官網(wǎng)和社區(qū)提供了好幾個web ui,但是那畢竟是別人寫的。改起來不簡單。我打算在下篇文章會介紹下, 如何自己實現(xiàn)一套簡單的docker rest api 接口。?
偶了 !
?本文轉(zhuǎn)自 rfyiamcool 51CTO博客,原文鏈接:http://blog.51cto.com/rfyiamcool/1539500,如需轉(zhuǎn)載請自行聯(lián)系原作者
總結(jié)
以上是生活随笔為你收集整理的使用python的docker-py实现docker的api操作的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Ext1.X的CheckboxSelec
- 下一篇: 『Python』__getattr__(