前沿:
听同事说,以后的dba申请可能有部分走其他部门的docker ,那边貌似在搞一个类似docker的平台管理系统,据说很霸道。于是乎,我自己也想尝试写一个简单的doker管理平台。 做为起步我先搞搞docker api,docker官网有个docker-py,用起来很是清爽简单。
首先安装docker的python相关的模块。
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:~#
咱们先查看下已经下载好的模块
[root@dev-ops ~]$docker images
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
查看已经启动的容器
[root@dev-ops ~]$docker ps -a
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
查看 docker 相关的版本
原文: http://rfyiamcool.blog.51cto.com/1030776/1539500
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 ~]$
好了,到现在为止 docker image有了,容器也有了,docker-py这个api也有了。。。
下面是我的操作的记录,有过shell操作经验的人,应该就很好上手的。
[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
创建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
这里是查看已经部署好的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 字眼的,结果没有搜到。 但是搜ubuntu_sshd有关的镜像会发现有的。
原文: 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看下他的运行状态
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看下刚才创建的结果。 我们在用python docker api的时候,创建了一个叫test_nima的容器。
篇幅的问题就不多说了, api几乎包含了docker的功能,最少能满足我的应用,及在开发中与运维平台的对接。朋友的公司已经在用纯node.js写了套api,为公司内部服务。其实官网和社区提供了好几个web ui,但是那毕竟是别人写的。改起来不简单。我打算在下篇文章会介绍下, 如何自己实现一套简单的docker rest api 接口。
偶了 !
排版都乱了,建议使用下js插件显示代码