2013-03-27 05:25:42 +08:00
:title: Python Web app example
2013-03-26 10:52:52 +08:00
:description: Building your own python web app using docker
:keywords: docker, example, python, web app
.. _python_web_app:
2013-06-02 13:03:28 +08:00
Python Web App
==============
2013-04-07 22:23:00 +08:00
.. include :: example_header.inc
2013-03-27 03:14:58 +08:00
The goal of this example is to show you how you can author your own docker images using a parent image, making changes to it, and then saving the results as a new image. We will do that by making a simple hello flask web application image.
2013-03-26 10:52:52 +08:00
**Steps:**
.. code-block :: bash
2013-03-29 02:50:00 +08:00
docker pull shykes/pybuilder
2013-03-26 10:52:52 +08:00
2013-03-27 21:44:20 +08:00
We are downloading the "shykes/pybuilder" docker image
2013-03-26 10:52:52 +08:00
.. code-block :: bash
2013-03-29 02:50:00 +08:00
URL=http://github.com/shykes/helloflask/archive/master.tar.gz
2013-03-26 10:52:52 +08:00
We set a URL variable that points to a tarball of a simple helloflask web app
.. code-block :: bash
2013-03-29 03:48:32 +08:00
BUILD_JOB=$(docker run -d -t shykes/pybuilder:latest /usr/local/bin/buildapp $URL)
2013-03-26 10:52:52 +08:00
2013-03-29 02:51:10 +08:00
Inside of the "shykes/pybuilder" image there is a command called buildapp, we are running that command and passing the $URL variable from step 2 to it, and running the whole thing inside of a new container. BUILD_JOB will be set with the new container_id.
2013-03-26 10:52:52 +08:00
.. code-block :: bash
2013-03-29 02:50:00 +08:00
docker attach $BUILD_JOB
2013-03-26 10:52:52 +08:00
[...]
We attach to the new container to see what is going on. Ctrl-C to disconnect
.. code-block :: bash
2013-05-22 11:45:27 +08:00
BUILD_IMG=$(docker commit $BUILD_JOB _/builds/github.com/shykes/helloflask/master)
2013-03-26 10:52:52 +08:00
Save the changed we just made in the container to a new image called "_/builds/github.com/hykes/helloflask/master" and save the image id in the BUILD_IMG variable name.
.. code-block :: bash
2013-03-29 03:48:32 +08:00
WEB_WORKER=$(docker run -d -p 5000 $BUILD_IMG /usr/local/bin/runapp)
2013-03-26 10:52:52 +08:00
2013-04-08 23:39:30 +08:00
- **"docker run -d "** run a command in a new container. We pass "-d" so it runs as a daemon.
2013-04-24 03:04:53 +08:00
- **"-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 23:39:30 +08: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-03-26 10:52:52 +08:00
Use the new image we just created and create a new container with network port 5000, and return the container id and store in the WEB_WORKER variable.
.. code-block :: bash
2013-03-29 02:50:00 +08:00
docker logs $WEB_WORKER
2013-05-22 11:45:27 +08:00
* Running on http://0.0.0.0:5000/
2013-03-26 10:52:52 +08: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-04-08 23:39:30 +08:00
.. code-block :: bash
WEB_PORT=$(docker port $WEB_WORKER 5000)
lookup the public-facing port which is NAT-ed store the private port used by the container and store it inside of the WEB_PORT variable.
.. code-block :: bash
2013-05-23 03:52:14 +08:00
# install curl if necessary, then ...
2013-05-23 03:54:50 +08:00
curl http://127.0.0.1:$WEB_PORT
2013-04-08 23:39:30 +08:00
Hello world!
access the web app using curl. If everything worked as planned you should see the line "Hello world!" inside of your console.
2013-03-26 10:52:52 +08: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-27 03:14:58 +08:00
2013-04-04 13:21:57 +08:00
Continue to :ref: `running_ssh_service` .