Detailed use of Docker in Python and Ipython

Source: Internet
Author: User
Tags iterable docker run
Now that Docker is one of the hottest projects on earth, it means that people actually like it not just because of that.

That said, I like to use containers, service discoveries and all the new ideas and fields that have been created to switch jobs as examples.

In this article I'll briefly describe using the Docker-py module in Python to manipulate the Docker container, which uses my favorite programming tool, Ipython.

Installing Docker-py

First, you need docker-py. Note that in this case I will be using Ubuntu trusty version 14.04.
Copy the Code code as follows:


$ pip Install Docker-py

Ipyhton

I really like using Ipython to explore Python. It's like a high-level Python Shell, but it can do more.
Copy the Code code as follows:


$ 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]:

Installing Docker

If Docker is not installed, first install Docker
Copy the Code code as follows:


$ sudo apt-get install Docker.io


And then put Docker.io up in a separate name Docker.
Copy CodeThe code is as follows:


$ 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 should now have a socket open that we can use to connect.
Copy CodeThe code is as follows:


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

Pull Mirror

Let's download BusyBox image
Copy the Code code as follows:


$ 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


Now we're ready to use the docker-py.

Using Docker-py

Now that we have docker-py, IPython, Docker, and busybox mirrors, we can build some containers.

If you are not familiar with Ipython, you can refer to this tutorial to learn (http://ipython.org/ipython-doc/stable/interactive/tutorial.html),
The Ipython is very powerful.

First, start a Ipython and import the Docker module.
Copy the Code code as follows:


$ 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


Then we build a connection to Docker
Copy CodeThe code is as follows:


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


Now we are connected to Docker.

Ipython Use the TAB key to complete the fill. If you enter "C." and then press the TAB key, Ipython displays all the methods and properties of the Docker connection object.
Copy the Code code as follows:


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


Let's take a look at the c.images I enter a "?" After C. Ipython will provide detailed information about this object.
Copy CodeThe code is as follows:


In [5]: C.images?
Type:instancemethod
String Form: >
File:/usr/local/lib/python2.7/dist-packages/docker/client.py
Definition:c.images (self, name=none, Quiet=false, All=false, Viz=false)
DocString:


Gets the busybox image.
Copy CodeThe code is as follows:


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 add a command that can be run, with the "env" command.
Copy CodeThe code is as follows:


In [8]: C.create_container (image= "BusyBox", command= "env")
OUT[8]:
{u ' Id ': U ' 584459a09e6d4180757cb5c10ac354ca46a32bf8e122fa3fb71566108f330c87 ',
U ' Warnings ': None}


Use ID to start this container
Copy CodeThe code is as follows:


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


We can check the logs and we should be able to see the output of the "env" command we configured when the container was created.
Copy CodeThe code is as follows:


In [All]: 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 the Docker command line to run a container with the same command-line options, you should see similar information.
Copy CodeThe code is as follows:


$ Docker Run BusyBox Env
home=/
Path=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
Hostname=ce3ad38a52bf


As far as I know, docker-py does not have a run option, we can only create a container and then start it.

Here is a case:
Copy the Code code as follows:


in [+]: BusyBox = C.create_container (image= "BusyBox", command= "echo Hi")

In []: 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 [+]: C.start (Busybox.get ("Id"))

In []: C.logs (Busybox.get ("Id"))
OUT[20]: ' hi\n '


If you have not used the busybox image, I suggest you use it. I also recommend Debain under the Jessie image, which is only 120MB, smaller than the Ubuntu image.

Summarize

Docker is an appealing new system that can be used to build interesting new technology applications, especially cloud-based services related. Using Ipython we explored how to use the
Docker-py module to create a Docker container. Now using Python, we can combine Docker and easily create a lot of new ideas.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.