[Docker] Using Docker in Python and IPython
Docker is one of the hottest projects on the planet, which means that people don't like it because of it.
Even so, I like using containers, service discovery, and all the interesting ideas and fields created to switch jobs as an example.
In this article, I will briefly introduce how to use the docker-py module in python to operate Docker containers. Here I will use IPython, my favorite programming tool.
Install docker-py
Docker-py is required first. Note that I will use Ubuntu Trusty 14.04 in this case.
$ pip install docker-py
IPyhton
I really like using IPython to explore Python. It looks like a total of advanced python shells, but more can be done.
$ 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]:
Install docker
If Docker is not installed, first install docker
$ sudo apt-get install docker.io
Start docker. io with an alias 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 should now have a socket enabled for connection.
$ ls /var/run/docker.sock/var/run/docker.sock
Pull Image
Let's download the busybox Image
$ 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
Now we are ready to use docker-py.
Use docker-py
Now we have docker-py, IPython, Docker, and busybox images, we can create some containers.
If you are not familiar with IPython, you can refer to this tutorial (http://ipython.org/ipython-doc/stable/interactive/tutorial.html ),
IPython is very powerful.
First, start IPython and import the docker module.
$ 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
Then we create a connection to Docker
In [2]: c = docker.Client(base_url='unix://var/run/docker.sock', ...: version='1.9', ...: timeout=10)
Now we have connected to Docker.
IPython uses the tab key to complete. If you enter "c." and press the tab key, IPython displays all methods and properties of the Docker connection object.
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
Let's take a look at c. images. I enter a "?" After c., ipython provides detailed information about this object.
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:
Obtain the busybox image.
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}]
Create a container. Note that I want to add a command that can be run. Here the "env" command is used.
In [8]: c.create_container(image="busybox", command="env")Out[8]:{u'Id': u'584459a09e6d4180757cb5c10ac354ca46a32bf8e122fa3fb71566108f330c87', u'Warnings': None}
Use ID to start the container
In [9]: c.start(container="584459a09e6d4180757cb5c10ac354ca46a32bf8e122fa3fb71566108f330c87")
We can check the log to see the output of the configured "env" command when the container is created.
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'
If you use docker command line and use the same command line option to run a container, You can see similar information.
$ docker run busybox envHOME=/PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/binHOSTNAME=ce3ad38a52bf
As far as I know, docker-py has no running options. We can create only one container and start it.
The following is a case:
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'
If you have not used a busybox image, we recommend that you use it. I also recommend the jessie image under debain, which is only 120 MB, smaller than the Ubuntu image.
Summary
Docker is an attractive new system that can be used to build interesting new technology applications, especially cloud services. Using IPython, we explored how to use
Docker-py module to create a docker container. Now using python, we can combine docker and easily create many new ideas.
Original
Address: http://serverascode.com/2014/06/05/docker-python.html
Orangleliu @ 2014-07-13