Dockerizing a simple Python Web application

September 25, 2015

Let's Dockerize a simple Flask-based Python Web application.

Dockerfile:

FROM debian:sid

RUN apt-get -y update
RUN apt-get install -y python python-pip

RUN pip install Flask

COPY . /hello-python-web

CMD ["python", "/hello-python-web/server.py"]

Our Docker image will:

Let's create a Docker image with docker build:

$ docker build -t hello-python-web .

We can inspect the Docker image with docker images:

$ docker images
REPOSITORY        TAG     IMAGE ID      CREATED        VIRTUAL SIZE
hello-python-web  latest  7684dc73147e  3 minutes ago  731.7 MB
debian            sid     e7d52d7d94ef  2 weeks ago    126.5 MB

We can run hello-python-web with docker run:

$ docker run -p 8080:8080 hello-python-web
 * Running on http://0.0.0.0:8080/ (Press CTRL+C to quit)

The server is now available at http://127.0.0.1:8080/.

$ curl -i -s localhost:8080
HTTP/1.0 200 OK
Content-Type: text/html; charset=utf-8
Content-Length: 13
Server: Werkzeug/0.10.4 Python/2.7.10
Date: Fri, 25 Sep 2015 00:12:38 GMT

Hello, world!

Tag the Docker image with docker tag:

$ docker tag 7684dc73147e earldouglas/hello-python-web:1.0.0

We can again inspect the Docker tag with docker images:

$ docker images
REPOSITORY                    TAG     IMAGE ID      CREATED         VIRTUAL SIZE
hello-python-web              latest  7684dc73147e  24 minutes ago  731.7 MB
earldouglas/hello-python-web  1.0.0   7684dc73147e  24 minutes ago  731.7 MB
debian                        sid     e7d52d7d94ef  2 weeks ago     126.5 MB

If necessary, sign-in to Docker with docker login:

$ docker login

Finally, publish the Docker image with docker push:

$ docker push earldouglas/hello-python-web