您好,欢迎访问一九零五行业门户网

使用IPython来操作Docker容器的入门指引

docker
现在docker是地球上最炙手可热的项目之一,就意味着人民实际上不仅仅是因为这个才喜欢它。
话虽如此,我非常喜欢使用容器,服务发现以及所有被创造出的新趣的点子和领域来切换工作作为范例。
这个文章中我会简要介绍使用python中的docker-py模块来操作docker 容器,这里会使用我喜爱的编程工具ipython。
安装docker-py
首先需要docker-py。注意这里的案例中我将会使用ubuntu trusty 14.04版本。
$ pip install docker-py
ipyhton
我真的很喜欢用ipython来探索python。 它像是一共高级的python shell,但是可以做的更多。
$ sudo apt-get install ipythonsnip!$ ipythonpython 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]:
安装 docker
如果没有安装docker,那首先安装docker
$ sudo apt-get install docker.io
然后把 docker.io 起个别名 docker
$ alias docker='docker.io'$ docker versionclient version: 0.9.1go version (client): go1.2.1git commit (client): 3600720server version: 0.9.1git commit (server): 3600720go version (server): go1.2.1last stable version: 0.11.1, please update docker
docker现在应该有个socket开启,我们可以用来连接。
$ ls /var/run/docker.sock/var/run/docker.sock
pull 镜像
让我们下载 busybox镜像
$ docker pull busyboxpulling repository busybox71e18d715071: download complete98b9fdab1cb6: download complete1277aa3f93b3: download complete6e0a2595b580: download complete511136ea3c5a: download completeb6c0d171b362: download complete8464f9ac64e8: download complete9798716626f6: download completefc1343e2fca0: download completef3c823ac7aa6: download complete
现在我们准备使用 docker-py 了。
使用 docker-py
现在我们有了docker-py , ipython, docker 和 busybox 镜像,我们就能建立一些容器。
如果你不是很熟悉ipython,可以参照这个教程学习(http://ipython.org/ipython-doc/stable/interactive/tutorial.html),
ipython是十分强大的。
首先启动一个ipython ,导入docker模块。
$ ipythonpython 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
in [2]: c = docker.client(base_url='unix://var/run/docker.sock', ...: version='1.9', ...: timeout=10)
现在我们已经连接到docker。
ipython使用tab键来补全的。 如果 输入 “c.” 然后按下tab键,ipython会显示docker连接对象所有的方法和属性。
in [3]: c.c.adapters c.headers c.pullc.attach c.history c.pushc.attach_socket c.hooks c.putc.auth c.images c.remove_containerc.base_url c.import_image c.remove_imagec.build c.info c.requestc.cert c.insert c.resolve_redirectsc.close c.inspect_container c.restartc.commit c.inspect_image c.searchc.containers c.kill c.sendc.cookies c.login c.startc.copy c.logs c.stopc.create_container c.max_redirects c.streamc.create_container_from_config c.mount c.tagc.delete c.options c.topc.diff c.params c.trust_envc.events c.patch c.verifyc.export c.port c.versionc.get c.post c.waitc.get_adapter c.prepare_requestc.head c.proxies
让我们来看下c.images 我输入一个 “?”在c.之后,ipython 会提供这个对象的详细信息。
in [5]: c.images?type: instancemethodstring form:file: /usr/local/lib/python2.7/dist-packages/docker/client.pydefinition: c.images(self, name=none, quiet=false, all=false, viz=false)docstring:
获取busybox 镜像。
in [6]: c.images(name=busybox)out[6]:[{u'created': 1401402591, u'id': u'71e18d715071d6ba89a041d1e696b3d201e82a7525fbd35e2763b8e066a3e4de', u'parentid': u'8464f9ac64e87252a91be3fbb99cee20cda3188de5365bec7975881f389be343', u'repotags': [u'busybox:buildroot-2013.08.1'], u'size': 0, u'virtualsize': 2489301}, {u'created': 1401402590, u'id': u'1277aa3f93b3da774690bc4f0d8bf257ff372e23310b4a5d3803c180c0d64cd5', u'parentid': u'f3c823ac7aa6ef78d83f19167d5e2592d2c7f208058bc70bf5629d4bb4ab996c', u'repotags': [u'busybox:ubuntu-14.04'], u'size': 0, u'virtualsize': 5609404}, {u'created': 1401402589, u'id': u'6e0a2595b5807b4f8c109f3c6c5c3d59c9873a5650b51a4480b61428427ab5d8', u'parentid': u'fc1343e2fca04a455f803ba66d1865739e0243aca6c9d5fd55f4f73f1e28456e', u'repotags': [u'busybox:ubuntu-12.04'], u'size': 0, u'virtualsize': 5454693}, {u'created': 1401402587, u'id': u'98b9fdab1cb6e25411eea5c44241561326c336d3e0efae86e0239a1fe56fbfd4', u'parentid': u'9798716626f6ae4e6b7f28451c0a1a603dc534fe5d9dd3900150114f89386216', u'repotags': [u'busybox:buildroot-2014.02', u'busybox:latest'], u'size': 0, u'virtualsize': 2433303}]
建立一个容器。 注意我添加一个可以将要运行的命令,这里用的是”env”命令。
in [8]: c.create_container(image=busybox, command=env)out[8]:{u'id': u'584459a09e6d4180757cb5c10ac354ca46a32bf8e122fa3fb71566108f330c87', u'warnings': none}
使用id来启动这个容器
in [9]: c.start(container=584459a09e6d4180757cb5c10ac354ca46a32bf8e122fa3fb71566108f330c87)
我们可以检查日志,应该可以看到当容器创建的时候 ,我们配置的”env”命令的输出。
in [11]: c.logs(container=584459a09e6d4180757cb5c10ac354ca46a32bf8e122fa3fb71566108f330c87)out[11]: 'home=/\npath=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin\nhostname=584459a09e6d\n'
如果使用docker命令行,使用同样的命令行选项运行一个容器,应该可以看到类似的信息。
$ docker run busybox envhome=/path=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/binhostname=ce3ad38a52bf
据我所知,docker-py没有运行选项,我们只能创建一个容器然后启动它。
以下是一个案例:
in [17]: busybox = c.create_container(image=busybox, command=echo hi)in [18]: busybox?type: dictstring form:{u'id': u'34ede853ee0e95887ea333523d559efae7dcbe6ae7147aa971c544133a72e254', u'warnings': none}length: 2docstring:dict() -> new empty dictionarydict(mapping) -> new dictionary initialized from a mapping object's (key, value) pairsdict(iterable) -> new dictionary initialized as if via: d = {} for k, v in iterable: d[k] = vdict(**kwargs) -> new dictionary initialized with the name=value pairs in the keyword argument list. for example: dict(one=1, two=2)in [19]: c.start(busybox.get(id))in [20]: c.logs(busybox.get(id))out[20]: 'hi\n'
如果你还没有使用过busybox镜像,我建议你使用下。我也建议debain下的jessie镜像,它只有120mb,比ubuntu镜像要小。
总结
docker是一个吸引人的新系统,可以用来建立有趣的新技术应用,特别是云服务相关的。使用ipython我们探索了怎么使用
docker-py模块来创建docker 容器。 现在使用python,我们可以结合docker和容易 创造出很多新的点子。
其它类似信息

推荐信息