Getting started with Ipython to operate Docker containers

Source: Internet
Author: User
Tags docker run
Docker

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.

$ 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.

$ sudo apt-get install ipythonsnip!$ ipythonpython 2.7.6 (default, Mar, 22:59:56) Type "copyright", "credits" or "L Icense "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 Sys Tem.object?  Details about ' object ', use ' object? ' for extra details. In [1]:

Installing Docker

If Docker is not installed, first install Docker

$ sudo apt-get install Docker.io

And then put Docker.io up in a separate name Docker.

$ alias docker= ' Docker.io ' $ docker versionclient Version:0.9.1go version (client): Go1.2.1git commit (client): 3600720Ser Ver version:0.9.1git commit (server): 3600720Go version (server): Go1.2.1last stable version:0.11.1, please update Docke R

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

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

Pull Mirror

Let's download 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'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.

$ Ipythonpython 2.7.6 (default, Mar, 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 Sys Tem.object?  Details about ' object ', use ' object? ' for extra details. In [1]: Import Docker

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 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.

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_ima GE 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.cont             Ainers 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.t Agc.delete c.options c.topc.diff c.params c.trust_envc.events C.PA tch 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 the c.images I enter a "?" After C. Ipython will provide 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: 
  
   
 
  

Gets 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 add a command that can be run, with 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 logs and we should be able to see the output of the "env" command we configured when the container was created.

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.

$ 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 does not have a run option, 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:    dictstring form:{u ' Id ': U ' 34ede853ee0e95887ea333523d559efae7dcbe6ae7147aa971c544133a72e254 ', U ' Warnings ': none}length:   2docstring:dict (), New empty dictionarydict (mapping), new dictionary initialized from a MAPP ing object ' s  (key, value) Pairsdict (iterable), new dictionary initialized as if via:  d = {}  for K, V in I  Terable:    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 [+]: 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.

  • 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.