A primer on using IPython to operate the Docker container

  • 2020-05-07 19:57:22
  • OfStack

Now Docker is one of the hottest projects on the planet, which means people actually like it for more than that.
Having said that, I really like using containers, service discovery, and all the interesting new ideas and domains that are being created to switch jobs as examples.
In this article I will briefly introduce the Docker container using the docker-py module in python, which USES my favorite programming tool IPython.
Install 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 enjoy exploring Python with IPython. It's like an python Shell, but it can do a lot more.


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

installs docker

If Docker is not installed, install docker first


$ sudo apt-get install docker.io

Then separate docker.io into 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
Last stable version: 0.11.1, please update docker

Docker should now have an socket enabled that we can use to connect.


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

Pull mirrors

Let's download the busybox image


$ 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 are ready to use docker-py.

USES docker-py

Now that we have docker-py, IPython, Docker and busybox images, we can set up some containers.
If you are not familiar with IPython, can consult the tutorial learning (http: / / ipython org/ipython - doc stable/interactive/tutorial html),
The IPython is 10 strong.

Start with one IPython and import the docker module.


$ 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 set up 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 USES the tab key to complete. If you type "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_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

So let's look at c.images and I'll type in 1 "?" After c., ipython provides the details of this 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>

Get the busybox image.


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

0

Set up a container. Notice that I added a command that can be run, using the "env" command.


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

1

Use ID to start the container


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

2

We can check the log and should see the output of the "env" command that we configured when the container was 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 the docker command line and run 1 container with the same command line options, you should see similar information.


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

4

As far as I know, docker-py has no run options, so we can only create one container and start it.

Here is one case:


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

5

If you haven't used busybox images, I suggest you do. I also recommend jessie mirror under debain, which is only 120MB, smaller than Ubuntu mirror.

summary

Docker is an attractive new system for building interesting new technology applications, especially those related to cloud services. Using IPython we explored how to use it
The docker-py module creates the docker container. Now using python, we can combine docker and easily create many new ideas.


Related articles: