欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用IPython來(lái)操作Docker容器的入門指引

 更新時(shí)間:2015年04月08日 10:10:15   投稿:goldensun  
這篇文章主要介紹了使用IPython來(lái)操作Docker容器的方法,包括一些基本的搭建和連接,主要依靠docker-py模塊,需要的朋友可以參考下

現(xiàn)在Docker是地球上最炙手可熱的項(xiàng)目之一,就意味著人民實(shí)際上不僅僅是因?yàn)檫@個(gè)才喜歡它。
話雖如此,我非常喜歡使用容器,服務(wù)發(fā)現(xiàn)以及所有被創(chuàng)造出的新趣的點(diǎn)子和領(lǐng)域來(lái)切換工作作為范例。
這個(gè)文章中我會(huì)簡(jiǎn)要介紹使用python中的docker-py模塊來(lái)操作Docker 容器,這里會(huì)使用我喜愛(ài)的編程工具IPython。
安裝docker-py

首先需要docker-py。注意這里的案例中我將會(huì)使用Ubuntu Trusty 14.04版本。

$ pip install docker-py

IPyhton

我真的很喜歡用IPython來(lái)探索Python。 它像是一共高級(jí)的python Shell,但是可以做的更多。

$ sudo apt-get install ipython
SNIP!
$ 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]:

安裝 docker

如果沒(méi)有安裝Docker,那首先安裝docker

$ sudo apt-get install docker.io

然后把 docker.io 起個(gè)別名 docker

$ alias docker='docker.io'
$ docker version
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: 0.11.1, please update docker

Docker現(xiàn)在應(yīng)該有個(gè)socket開(kāi)啟,我們可以用來(lái)連接。

$ ls /var/run/docker.sock
/var/run/docker.sock

Pull 鏡像

讓我們下載 busybox鏡像

$ docker pull busybox
Pulling repository busybox
71e18d715071: Download complete
98b9fdab1cb6: Download complete
1277aa3f93b3: Download complete
6e0a2595b580: Download complete
511136ea3c5a: Download complete
b6c0d171b362: Download complete
8464f9ac64e8: Download complete
9798716626f6: Download complete
fc1343e2fca0: Download complete
f3c823ac7aa6: Download complete

現(xiàn)在我們準(zhǔn)備使用 docker-py 了。

使用 docker-py

現(xiàn)在我們有了docker-py , IPython, Docker 和 busybox 鏡像,我們就能建立一些容器。
如果你不是很熟悉IPython,可以參照這個(gè)教程學(xué)習(xí)(http://ipython.org/ipython-doc/stable/interactive/tutorial.html),
IPython是十分強(qiáng)大的。

首先啟動(dòng)一個(gè)IPython ,導(dǎo)入docker模塊。

$ 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

然后我們建立一個(gè)連接到Docker

In [2]: c = docker.Client(base_url='unix://var/run/docker.sock',
  ...:          version='1.9',
  ...:          timeout=10)

現(xiàn)在我們已經(jīng)連接到Docker。

IPython使用tab鍵來(lái)補(bǔ)全的。 如果 輸入 “c.” 然后按下tab鍵,IPython會(huì)顯示Docker連接對(duì)象所有的方法和屬性。

In [3]: c.
c.adapters           c.headers            c.pull
c.attach            c.history            c.push
c.attach_socket         c.hooks             c.put
c.auth             c.images            c.remove_container
c.base_url           c.import_image         c.remove_image
c.build             c.info             c.request
c.cert             c.insert            c.resolve_redirects
c.close             c.inspect_container       c.restart
c.commit            c.inspect_image         c.search
c.containers          c.kill             c.send
c.cookies            c.login             c.start
c.copy             c.logs             c.stop
c.create_container       c.max_redirects         c.stream
c.create_container_from_config c.mount             c.tag
c.delete            c.options            c.top
c.diff             c.params            c.trust_env
c.events            c.patch             c.verify
c.export            c.port             c.version
c.get              c.post             c.wait
c.get_adapter          c.prepare_request
c.head             c.proxies

讓我們來(lái)看下c.images 我輸入一個(gè) “?”在c.之后,ipython 會(huì)提供這個(gè)對(duì)象的詳細(xì)信息。

In [5]: c.images?
Type:    instancemethod
String Form:<bound method Client.images of <docker.client.Client object at 0x7f3acc731790>>
File:    /usr/local/lib/python2.7/dist-packages/docker/client.py
Definition: c.images(self, name=None, quiet=False, all=False, viz=False)
Docstring: <no 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}]

建立一個(gè)容器。 注意我添加一個(gè)可以將要運(yùn)行的命令,這里用的是”env”命令。

In [8]: c.create_container(image="busybox", command="env")
Out[8]:
{u'Id': u'584459a09e6d4180757cb5c10ac354ca46a32bf8e122fa3fb71566108f330c87',
 u'Warnings': None}

使用ID來(lái)啟動(dòng)這個(gè)容器

In [9]: c.start(container="584459a09e6d4180757cb5c10ac354ca46a32bf8e122fa3fb71566108f330c87")

我們可以檢查日志,應(yīng)該可以看到當(dāng)容器創(chuàng)建的時(shí)候 ,我們配置的”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命令行,使用同樣的命令行選項(xiàng)運(yùn)行一個(gè)容器,應(yīng)該可以看到類似的信息。

$ docker run busybox env
HOME=/
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=ce3ad38a52bf

據(jù)我所知,docker-py沒(méi)有運(yùn)行選項(xiàng),我們只能創(chuàng)建一個(gè)容器然后啟動(dòng)它。

以下是一個(gè)案例:

In [17]: busybox = c.create_container(image="busybox", command="echo hi")

In [18]: busybox?
Type:    dict
String Form:{u'Id': u'34ede853ee0e95887ea333523d559efae7dcbe6ae7147aa971c544133a72e254', u'Warnings': None}
Length:   2
Docstring:
dict() -> new empty dictionary
dict(mapping) -> new dictionary initialized from a mapping object's
  (key, value) pairs
dict(iterable) -> new dictionary initialized as if via:
  d = {}
  for k, v in iterable:
    d[k] = v
dict(**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'

如果你還沒(méi)有使用過(guò)busybox鏡像,我建議你使用下。我也建議debain下的jessie鏡像,它只有120MB,比Ubuntu鏡像要小。

總結(jié)

Docker是一個(gè)吸引人的新系統(tǒng),可以用來(lái)建立有趣的新技術(shù)應(yīng)用,特別是云服務(wù)相關(guān)的。使用IPython我們探索了怎么使用
docker-py模塊來(lái)創(chuàng)建docker 容器。 現(xiàn)在使用python,我們可以結(jié)合docker和容易 創(chuàng)造出很多新的點(diǎn)子。

相關(guān)文章

最新評(píng)論