Docker Python


In [ ]:
# Tutorial

from IPython.core.display import HTML
# https://www.digitalocean.com/community/tutorials/docker-explained-how-to-containerize-python-web-applications
HTML("<iframe src=https://www.digitalocean.com/community/tutorials/docker-explained-how-to-containerize-python-web-applications </iframe>")

In [ ]:
# Tutorial - Create a Docker Image for a Python App

from IPython.core.display import HTML
# https://learn.tutum.co/tag/1/python/getting-started/1/creating-an-image-for-your-python-app
HTML("<iframe src=learn.tutum.co/tag/1/python/getting-started/1/creating-an-image-for-your-python-app </iframe>")

In [ ]:
# Docker API

from IPython.core.display import HTML
# https://github.com/docker/docker-py
HTML("<iframe src=https://github.com/docker/docker-py </iframe>")

Install Docker


In [ ]:
# Docker-py

pip install docker-py

In [ ]:
#Pull Python from a Docker Image

$ docker pull python

In [ ]:
$ sudo apt-get install docker.io

$ 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

In [ ]:
#!/usr/bin/env python
import os
import sys
from setuptools import setup

ROOT_DIR = os.path.dirname(__file__)
SOURCE_DIR = os.path.join(ROOT_DIR)

requirements = [
    'requests >= 2.2.1, < 2.5.0',
    'six >= 1.3.0',
]

if sys.version_info[0] < 3:
    requirements.append('websocket-client >= 0.11.0')

exec(open('docker/version.py').read())

with open('./test-requirements.txt') as test_reqs_txt:
    test_requirements = [line for line in test_reqs_txt]


setup(
    name="docker-py",
    version=version,
    description="Python client for Docker.",
    packages=['docker', 'docker.auth', 'docker.unixconn', 'docker.utils',
              'docker.ssladapter'],
    install_requires=requirements,
    tests_require=test_requirements,
    zip_safe=False,
    test_suite='tests',
    classifiers=[
        'Development Status :: 4 - Beta',
        'Environment :: Other Environment',
        'Intended Audience :: Developers',
        'Operating System :: OS Independent',
        'Programming Language :: Python',
        'Programming Language :: Python :: 2.6',
        'Programming Language :: Python :: 2.7',
        'Programming Language :: Python :: 3.2',
        'Programming Language :: Python :: 3.3',
        'Programming Language :: Python :: 3.4',
        'Topic :: Utilities',
        'License :: OSI Approved :: Apache Software License',

Install iPython in Ubuntu


In [ ]:
# Ubuntu

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

Dockerize a Python Web App in AWS


In [ ]:
from IPython.core.display import HTML
# http://blogs.aws.amazon.com/application-management/post/Tx1ZLAHMVBEDCOC/Dockerizing-a-Python-Web-App
HTML("<iframe src=http://blogs.aws.amazon.com/application-management/post/Tx1ZLAHMVBEDCOC/Dockerizing-a-Python-Web-App </iframe>")

Start iPython and Import Docker


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

Pull BusyBox Image


In [ ]:
$ docker pull busybox

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

In [ ]:
import docker

Docker Socket


In [ ]:
$ ls /var/run/docker.sock 
/var/run/docker.sock

Connect to Docker


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

Create a Container


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

#Out[8]: {u'Id': u'584459a09e6d4180757cb5c10ac354ca46a32bf8e122fa3fb71566108f330c87',
 u'Warnings': None}

Start Container using ID


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

In [ ]:
$ docker run busybox env