summaryrefslogtreecommitdiff
path: root/docs/sources/examples/python_web_app.rst
blob: 33c038f9abf498b5e966903619cc7cf228369962 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
:title: Python Web app example
:description: Building your own python web app using docker
:keywords: docker, example, python, web app

.. _python_web_app:

Python Web App
==============

.. include:: example_header.inc

While using Dockerfiles is the preferred way to create maintainable
and repeatable images, its useful to know how you can try things out
and then commit your live changes to an image.

The goal of this example is to show you how you can modify your own
Docker images  by making changes to a running 
container, and then saving the results as a new image. We will do 
that by making a simple 'hello world' Flask web application image.

Download the initial image
--------------------------

Download the ``shykes/pybuilder`` Docker image from the ``http://index.docker.io``
registry. 

This image contains a ``buildapp`` script to download the web app and then ``pip install`` 
any required modules, and a ``runapp`` script that finds the ``app.py`` and runs it.

.. _`shykes/pybuilder`: https://github.com/shykes/pybuilder

.. code-block:: bash

    $ sudo docker pull shykes/pybuilder

.. note:: This container was built with a very old version of docker 
    (May 2013 - see `shykes/pybuilder`_ ), when the ``Dockerfile`` format was different, 
    but the image can still be used now.

Interactively make some modifications
-------------------------------------

We then start a new container running interactively  using the image. 
First, we set a ``URL`` variable that points to a tarball of a simple 
helloflask web app, and then we run a command contained in the image called
``buildapp``, passing it the ``$URL`` variable. The container is
given a name ``pybuilder_run`` which we will use in the next steps.

While this example is simple, you could run any number of interactive commands,
try things out, and then exit when you're done.

.. code-block:: bash

    $ sudo docker run -i -t --name pybuilder_run shykes/pybuilder bash

    $$ URL=http://github.com/shykes/helloflask/archive/master.tar.gz
    $$ /usr/local/bin/buildapp $URL
    [...]
    $$ exit

Commit the container to create a new image
------------------------------------------

Save the changes we just made in the container to a new image called
``/builds/github.com/shykes/helloflask/master``. You now have 3 different
ways to refer to the container: name ``pybuilder_run``, short-id ``c8b2e8228f11``, or 
long-id ``c8b2e8228f11b8b3e492cbf9a49923ae66496230056d61e07880dc74c5f495f9``.

.. code-block:: bash

    $ sudo docker commit pybuilder_run /builds/github.com/shykes/helloflask/master
    c8b2e8228f11b8b3e492cbf9a49923ae66496230056d61e07880dc74c5f495f9


Run the new image to start the web worker
-----------------------------------------

Use the new image to create a new container with
network port 5000 mapped to a local port

.. code-block:: bash

    $ sudo docker run -d -p 5000 --name web_worker /builds/github.com/shykes/helloflask/master /usr/local/bin/runapp


- **"docker run -d "** run a command in a new container. We pass "-d"
  so it runs as a daemon.
- **"-p 5000"** the web app is going to listen on this port, so it
  must be mapped from the container to the host system.
- **/usr/local/bin/runapp** is the command which starts the web app.


View the container logs
-----------------------

View the logs for the new ``web_worker`` container and
if everything worked as planned you should see the line ``Running on
http://0.0.0.0:5000/`` in the log output.

To exit the view without stopping the container, hit Ctrl-C, or open another 
terminal and continue with the example while watching the result in the logs.

.. code-block:: bash

    $ sudo docker logs -f web_worker
     * Running on http://0.0.0.0:5000/


See the webapp output
---------------------

Look up the public-facing port which is NAT-ed. Find the private port
used by the container and store it inside of the ``WEB_PORT`` variable.

Access the web app using the ``curl`` binary. If everything worked as planned you
should see the line ``Hello world!`` inside of your console.

.. code-block:: bash

    $ WEB_PORT=$(sudo docker port web_worker 5000 | awk -F: '{ print $2 }')

    # install curl if necessary, then ...
    $ curl http://127.0.0.1:$WEB_PORT
    Hello world!


Clean up example containers and images
--------------------------------------

.. code-block:: bash

    $ sudo docker ps --all

List ``--all`` the Docker containers. If this container had already finished
running, it will still be listed here with a status of 'Exit 0'.

.. code-block:: bash

    $ sudo docker stop web_worker
    $ sudo docker rm web_worker pybuilder_run
    $ sudo docker rmi /builds/github.com/shykes/helloflask/master shykes/pybuilder:latest

And now stop the running web worker, and delete the containers, so that we can 
then delete the images that we used.