DOCUMENTATION

Building a python web app

Note

This example assumes you have Docker running in daemon mode. For more information please see Running The Examples

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.

Steps:

docker pull shykes/pybuilder

We are downloading the “shykes/pybuilder” docker image

URL=http://github.com/shykes/helloflask/archive/master.tar.gz

We set a URL variable that points to a tarball of a simple helloflask web app

BUILD_JOB=$(docker run -d -t shykes/pybuilder:latest /usr/local/bin/buildapp $URL)

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.

docker attach $BUILD_JOB
[...]

We attach to the new container to see what is going on. Ctrl-C to disconnect

BUILD_IMG=$(docker commit $BUILD_JOB _/builds/github.com/hykes/helloflask/master)

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.

WEB_WORKER=$(docker run -d -p 5000 $BUILD_IMG /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.
  • “$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.

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.

docker logs $WEB_WORKER
 * Running on http://0.0.0.0:5000/

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.

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.

curl http://`hostname`:$WEB_PORT
  Hello world!

access the web app using curl. If everything worked as planned you should see the line “Hello world!” inside of your console.

Video:

See the example in action

Continue to Create an ssh daemon service.