Introduction Guide to using Ipython to manipulate Docker containers _python

Source: Internet
Author: User
Tags iterable docker run in python

Now that Docker is one of the hottest projects on earth, it means that people actually like it more than that.
Having said that, I like to use containers, service discovery, and all the new interesting ideas and fields that have been created to switch work as an example.
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.
Install Docker-py

First you need to docker-py. Note that in this case I will use the Ubuntu trusty 14.04 version.

$ pip Install Docker-py

Ipyhton

I really like to use Ipython to explore Python. It's like a high-level Python Shell, but it can do more.

$ sudo apt-get install Ipython
snip!
$ Ipython
Python 2.7.6 (default, Mar 2014, 22:59:56)
Type "Copyright", "credits" or "license" for more Informat Ion.

IPython 1.2.1--an enhanced Interactive Python.
?     -> Introduction and Overview of IPython ' s features.
%quickref-> Quick Reference.
Help   -> The 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

and docker.io up the individual names 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< C11/>last stable version:0.11.1, please update Docker

Docker should now have a socket open, we can use to connect.

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

Pull Mirroring

Let's download the BusyBox Mirror

$ docker Pull BusyBox
pulling repository busybox 71e18d715071:download complete 98b9fdab1cb6:download
Complete
1277aa3f93b3:download complete
6e0a2595b580:download complete
511136ea3c5a:download
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 mirroring, we can build some containers.
If you are not very 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.

$ Ipython
Python 2.7.6 (default, Mar 2014, 22:59:56)
Type "Copyright", "credits" or "license" for more Informat Ion.

IPython 1.2.1--an enhanced Interactive Python.
?     -> Introduction and Overview of IPython ' s features.
%quickref-> Quick Reference.
Help   -> The Python ' s own Help system.
Object?  -> details about ' object ', use ' object?? ' for extra details.

In [1]: Import Docker

And then we build 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 Use the TAB key to complement. If you enter "C." and then press the TAB key, Ipython displays all the methods and properties of the Docker connection object.

In [3]: C. c.adapters c.headers c.pull c.attach c.history c.push c.attach_sock ET c.hooks c.put c.auth c.images c.remove_container c.base_url c.impo Rt_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 Arch 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.even             TS c.patch c.verify c.export c.port c.version c.get 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 provides detailed information about the object.

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>

Gets the BusyBox mirror 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 ' 1277aa3f93b3da774690bc4f0d8bf257ff372e2331
 0B4A5D3803C180C0D64CD5 ', U ' parentid ': U ' f3c823ac7aa6ef78d83f19167d5e2592d2c7f208058bc70bf5629d4bb4ab996c ', U ' repotags ': [u ' busybox:ubuntu-14.04 '], U ' Size ': 0, U ' virtualsize ': 5609404}, {u ' Created ': 1401402589, U ' Id ': U ' 6e0a25 95b5807b4f8c109f3c6c5c3d59c9873a5650b51a4480b61428427ab5d8 ', 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. Notice that I add a command that I can run, and here is the "env" command.

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

Use ID to start this container

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

We can check the log and should see the output of the "env" command we configured when the container was created.

In [one]: 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.

$ 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 the option to run, we can only create a container and then start it.

Here is a case:

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 Ed as if via:
  d = {}
  for K, V in iterable:
    d[k] = v
dict (**kwargs)-> new dictionary initialized WI Th the Name=value pairs in the
  keyword argument list. For Example:dict (one=1, two=2) in

[[]: C.start (busybox.get ("id"))

: C.logs (busybox.get ("id"))
out[20]: ' hi\n '

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

Summarize

The

Docker is an attractive new system that can be used to create interesting new technology applications, especially for cloud 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.

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.