2013-03-26 21:25:42 +00:00
|
|
|
:title: Python Web app example
|
2013-03-25 19:52:52 -07:00
|
|
|
:description: Building your own python web app using docker
|
|
|
|
:keywords: docker, example, python, web app
|
|
|
|
|
|
|
|
.. _python_web_app:
|
|
|
|
|
2013-06-01 22:03:28 -07:00
|
|
|
Python Web App
|
|
|
|
==============
|
2013-04-07 10:23:00 -04:00
|
|
|
|
|
|
|
.. include:: example_header.inc
|
|
|
|
|
2013-08-13 18:05:35 -07:00
|
|
|
The goal of this example is to show you how you can author your own
|
2013-11-02 18:26:52 -07:00
|
|
|
Docker images using a parent image, making changes to it, and then
|
2013-08-13 18:05:35 -07:00
|
|
|
saving the results as a new image. We will do that by making a simple
|
2013-11-02 18:26:52 -07:00
|
|
|
hello Flask web application image.
|
2013-03-25 19:52:52 -07:00
|
|
|
|
|
|
|
**Steps:**
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
2013-08-13 18:05:35 -07:00
|
|
|
sudo docker pull shykes/pybuilder
|
2013-03-25 19:52:52 -07:00
|
|
|
|
2013-11-02 18:26:52 -07:00
|
|
|
We are downloading the ``shykes/pybuilder`` Docker image
|
2013-03-25 19:52:52 -07:00
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
2013-03-28 11:50:00 -07:00
|
|
|
URL=http://github.com/shykes/helloflask/archive/master.tar.gz
|
2013-03-25 19:52:52 -07:00
|
|
|
|
2013-11-02 18:26:52 -07:00
|
|
|
We set a ``URL`` variable that points to a tarball of a simple helloflask web app
|
2013-03-25 19:52:52 -07:00
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
2013-08-13 18:05:35 -07:00
|
|
|
BUILD_JOB=$(sudo docker run -d -t shykes/pybuilder:latest /usr/local/bin/buildapp $URL)
|
2013-03-25 19:52:52 -07:00
|
|
|
|
2013-11-02 18:26:52 -07:00
|
|
|
Inside of the ``shykes/pybuilder`` image there is a command called
|
|
|
|
``buildapp``, we are running that command and passing the ``$URL`` variable
|
2013-08-13 18:05:35 -07:00
|
|
|
from step 2 to it, and running the whole thing inside of a new
|
2013-11-02 18:26:52 -07:00
|
|
|
container. The ``BUILD_JOB`` environment variable will be set with the new container ID.
|
2013-03-25 19:52:52 -07:00
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
2013-11-07 14:06:49 -08:00
|
|
|
sudo docker attach -sig-proxy=false $BUILD_JOB
|
2013-03-25 19:52:52 -07:00
|
|
|
[...]
|
|
|
|
|
2013-08-13 18:05:35 -07:00
|
|
|
While this container is running, we can attach to the new container to
|
2013-11-07 14:06:49 -08:00
|
|
|
see what is going on. The flag ``-sig-proxy`` set as ``false`` allows you to connect and
|
|
|
|
disconnect (Ctrl-C) to it without stopping the container.
|
2013-08-11 17:27:47 -07:00
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
2013-08-13 18:05:35 -07:00
|
|
|
sudo docker ps -a
|
2013-11-02 18:26:52 -07:00
|
|
|
|
|
|
|
List all Docker containers. If this container has already finished
|
2013-08-13 18:05:35 -07:00
|
|
|
running, it will still be listed here.
|
2013-03-25 19:52:52 -07:00
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
2013-08-13 18:05:35 -07:00
|
|
|
BUILD_IMG=$(sudo docker commit $BUILD_JOB _/builds/github.com/shykes/helloflask/master)
|
2013-03-25 19:52:52 -07:00
|
|
|
|
2013-08-13 18:05:35 -07:00
|
|
|
Save the changes we just made in the container to a new image called
|
2013-11-02 18:26:52 -07:00
|
|
|
``_/builds/github.com/hykes/helloflask/master`` and save the image ID in
|
|
|
|
the ``BUILD_IMG`` variable name.
|
2013-03-25 19:52:52 -07:00
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
2013-08-13 18:05:35 -07:00
|
|
|
WEB_WORKER=$(sudo docker run -d -p 5000 $BUILD_IMG /usr/local/bin/runapp)
|
2013-03-25 19:52:52 -07:00
|
|
|
|
2013-08-13 18:05:35 -07:00
|
|
|
- **"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.
|
2013-04-08 17:39:30 +02:00
|
|
|
- **"$BUILD_IMG"** is the image we want to run the command inside of.
|
|
|
|
- **/usr/local/bin/runapp** is the command which starts the web app.
|
|
|
|
|
2013-08-13 18:05:35 -07:00
|
|
|
Use the new image we just created and create a new container with
|
2013-11-02 18:26:52 -07:00
|
|
|
network port 5000, and return the container ID and store in the
|
|
|
|
``WEB_WORKER`` variable.
|
2013-03-25 19:52:52 -07:00
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
2013-08-13 18:05:35 -07:00
|
|
|
sudo docker logs $WEB_WORKER
|
2013-05-21 21:45:27 -06:00
|
|
|
* Running on http://0.0.0.0:5000/
|
2013-03-25 19:52:52 -07:00
|
|
|
|
2013-11-02 18:26:52 -07:00
|
|
|
View the logs for the new container using the ``WEB_WORKER`` variable, and
|
|
|
|
if everything worked as planned you should see the line ``Running on
|
|
|
|
http://0.0.0.0:5000/`` in the log output.
|
2013-03-25 19:52:52 -07:00
|
|
|
|
2013-04-08 17:39:30 +02:00
|
|
|
.. code-block:: bash
|
|
|
|
|
2013-11-13 13:20:38 +01:00
|
|
|
WEB_PORT=$(sudo docker port $WEB_WORKER 5000 | awk -F: '{ print $2 }')
|
2013-04-08 17:39:30 +02:00
|
|
|
|
2013-08-13 18:05:35 -07:00
|
|
|
Look up the public-facing port which is NAT-ed. Find the private port
|
2013-11-02 18:26:52 -07:00
|
|
|
used by the container and store it inside of the ``WEB_PORT`` variable.
|
2013-04-08 17:39:30 +02:00
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
2013-05-22 12:52:14 -07:00
|
|
|
# install curl if necessary, then ...
|
2013-05-22 12:54:50 -07:00
|
|
|
curl http://127.0.0.1:$WEB_PORT
|
2013-04-08 17:39:30 +02:00
|
|
|
Hello world!
|
|
|
|
|
2013-11-02 18:26:52 -07:00
|
|
|
Access the web app using the ``curl`` binary. If everything worked as planned you
|
|
|
|
should see the line ``Hello world!`` inside of your console.
|
2013-03-25 19:52:52 -07:00
|
|
|
|
|
|
|
**Video:**
|
|
|
|
|
|
|
|
See the example in action
|
|
|
|
|
|
|
|
.. raw:: html
|
|
|
|
|
|
|
|
<div style="margin-top:10px;">
|
|
|
|
<iframe width="720" height="350" src="http://ascii.io/a/2573/raw" frameborder="0"></iframe>
|
|
|
|
</div>
|
2013-03-26 12:14:58 -07:00
|
|
|
|
2013-04-03 22:21:57 -07:00
|
|
|
Continue to :ref:`running_ssh_service`.
|