Use Docker in Python and IPython

Source: Internet
Author: User

Use Docker in Python and IPython

This article mainly introduces how to use Docker in Python and IPython. Docker is an attractive new system that can be used to build interesting new technology applications, especially cloud services, for more information, see

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.

The Code is as follows:

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

The Code is 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]:

Install docker

If Docker is not installed, first install docker

Copy the Code as follows:

$ Sudo apt-get install docker. io

Start docker. io with an alias docker.

Copy the Code 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 enabled for connection.

Copy the Code as follows:

$ Ls/var/run/docker. sock

/Var/run/docker. sock

Pull Image

Let's download the busybox Image

Copy the Code as follows:

$ Docker pull busybox

Pulling repository busybox

71e18d715071: Download complete

98b9fdab1cb6: Download complete

1277aa3f93b3: Download complete

6e0a2595b580: Download complete

2017136ea3c5a: Download complete

B6c0d171bte: Download complete

8464f9ac64e8: Download complete

9798716626f6: Download complete

Fc1343e2fca0: Download complete

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

Copy the 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 create a connection to Docker

Copy the Code as follows:

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.

The Code is 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 c. images. I enter a "?" After c., ipython provides detailed information about this object.

The 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:

Obtain the busybox image.

The Code is as follows:

In [6]: c. images (name = "busybox ")

Out [6]:

[{U 'created ': 1401402591,

U'id': u'shanghai ',

U'parentid': u'8464f9ac64e87252a91be3fbb99cee20cda3188de5365bec7975881f389be343 ',

U'repotags': [u'busybox: buildroot-2013.08.1 '],

U'size': 0,

U'virtualsize ': 2489301 },

{U 'created ': 1401402590,

U'id': u'1277aa3f93b3da774690bc4f0d8bf257ff372e23310b4a5dda-3c180c0d64cd5 ',

U'parentid': u'region ',

U'repotags': [u'busybox: ubuntu-14.04 '],

U'size': 0,

U'virtualsize ': 5609404 },

{U 'created ': 1401402589,

U'id': u'shanghai ',

U'parentid': u'region ',

U'repotags': [u'busybox: ubuntu-12.04 '],

U'size': 0,

U'virtualsize ': 5454693 },

{U 'created ': 1401402587,

U'id': u'shanghai ',

U'parentid': u'9798716626f6ae4e6b7f28451c0a1a603dc534fe5d9dd3900150114f89426216 ',

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.

The Code is as follows:

In [8]: c. create_container (image = "busybox", command = "env ")

Out [8]:

{U'id': u'hangzhou ',

U'warnings': None}

Use ID to start the container

The Code is as follows:

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

We can check the log to see the output of the configured "env" command when the container is created.

Copy the Code as follows:

In [11]: c. logs (container = "container ")

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.

Copy the Code 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 has no running options. We can create only one container and start it.

The following is a case:

Copy the Code as follows:

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

In [18]: busybox?

Type: dict

String Form: {u'id': u'hangzhou', 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:

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'

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.

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.