Docker: running simple Hello World web application

By | 18/02/2016

This post will focus on running an easy web application in a Docker container. We will run a simple web application that displays ‘Hello World’ in a web browser.

In order to do so, we will run a Docker container with the training/webapp base image which can be found here. Running the following command (see previous tutorial also) will launch a container with a Python Flask web app that returns ‘Hello World’ upon visiting the root URL.

root@ubuntu-demo:/home/cloud-user# docker run -d -P training/webapp python app.py
Unable to find image 'training/webapp:latest' locally
latest: Pulling from training/webapp
2880a3395ede: Pull complete
515565c29c94: Pull complete
98b15185dba7: Pull complete
2ce633e3e9c9: Pull complete
2ee0b8f351f7: Pull complete
2505b734adda: Pull complete
20dd0c759013: Pull complete
f95ebd363bf2: Pull complete
1952e3bf3d7e: Pull complete
abb991a4ed5e: Pull complete
7cbae6914197: Pull complete
f74dd040041e: Pull complete
54bb4e8718e8: Pull complete
Digest: sha256:06e9c1983bd6d5db5fba376ccd63bfa529e8d02f23d5079b8f74a616308fb11d
Status: Downloaded newer image for training/webapp:latest
baf2ec48bc809e58e9c23f187167a01877814d658c95dc50e7c8463a2885e77d

The big question now is how can I see the output using a web browser. After all, we were told this was a web application. So, the answer is pretty simple in fact. When running the ‘docker ps’ command, we see now also a PORTS section. It tells us that docker is binding the port ‘32768’ of our host system to port ‘5000’ on the container. Note that port 5000 is the default port for the Python Flask application.

root@ubuntu-demo:/home/cloud-user# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                     NAMES
baf2ec48bc80        training/webapp     "python app.py"     28 seconds ago      Up 22 seconds       0.0.0.0:32768->5000/tcp   mad_tesla

So we can now go ahead and test if we can see the web application return the Hello World string. As I’m running on an Openstack environment, I will need to modify my security group to allow access to port 32768. If you are running on a localhost, this might me needed. After opening port 32768, I can visit the :32768 and you’ll see something similar to the below screenshot. Just means that everything worked successfully. Great!
WebAppDocker

Note: a potentially easier way to return the port mapping is by doing:

root@ubuntu-demo:/home/cloud-user# docker port mad_tesla
5000/tcp -> 0.0.0.0:32768

Let’s take a look at the logs generated by our containerised web application. Easy enough!

root@ubuntu-demo:/home/cloud-user# docker logs -f mad_tesla
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
10.55.164.119 - - [13/Apr/2016 11:49:39] "GET / HTTP/1.1" 200 -
10.55.164.119 - - [13/Apr/2016 11:49:40] "GET /favicon.ico HTTP/1.1" 404 -
10.55.164.119 - - [13/Apr/2016 11:59:51] "GET / HTTP/1.1" 200 -
10.55.164.119 - - [13/Apr/2016 11:59:51] "GET /favicon.ico HTTP/1.1" 404 -

Leave a Reply

Your email address will not be published. Required fields are marked *