"Hello, World!" Python to Docker

"Hello, World!" Python to Docker

Dockerize your python application

Introduction

Every software engineer writes a "hello world" program at least once in their lifetime. It often is the first step in learning a new programming language or a new software framework. This blog is targeted to become one for your Docker journey.

Pre-requisites

  1. Basic knowledge of command line.

  2. Python

  3. Python venv

  4. Docker-Desktop

Hands-on

We will be using this git repo for this hands on - https://github.com/guptaachin/hello-world-to-kubernetes

At the end of this hands on you will have a working python web application and Docker image for that web-application.

Outline

  1. Preliminary directory set up.

  2. Write a simple "hello world" web application with Python.

  3. Dockerize "hello world" application.

Without further a do, let's dive in.

Preliminary directory setup

Let's quickly look the directory structure for this project

hello-world-to-kubernetes
└── src

There are numerous ways to create that directory structure.
One of the ways is to use command line. Here are quick commands if you are using a command line.

# Change dir to your preference.
mkdir hello-world-to-kubernetes # make dir "hello-world-to-kubernetes"
cd hello-world-to-kubernetes  # change dir "hello-world-to-kubernetes"
mkdir src                  # make "src" in "hello-world-to-kubernetes"

Write a simple "hello world" application with Python

With the directory structure in place. Let's write our "hello world" program in Python.

  1. Change in to src directory. cd src

  2. Create a simple "hello world" python script with Flask.

  3. Create a file app.py

# Simple py web app to print "Hello, World!" on browser.
from flask import Flask # import Flask

app = Flask(__name__)   # initialize Flask app

# Retun message "Hello, World!"
@app.route('/')
def hello_world():
    return 'Hello, World!'

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000, debug=True) # Run Flask app

Please do not worry. I understand that this looks different than print ("hello world" script. This is needed to nicely show our work at the end. As a bonus you get to learn a new popular web framework for Python.

Moving on, as you have observed, our web app uses Flask as a dependency.
Let's quickly create a requirements.txt file to manage this dependency.

  1. Create a file requirements.txt
Flask==3.0.1

Defining a requirements file along side python applications is a common practice to manage python applications dependencies.
All right ! let's see this in action.

  1. [Optional but encouraged] Create a script test-web-app.sh
#!/bin/bash

# Set the name of your virtual environment
VENV_NAME="my_py_web_app"

# Create a virtual environment
python3 -m venv $VENV_NAME

# Activate the virtual environment
source $VENV_NAME/bin/activate

# Install requirements using pip
pip install -r requirements.txt

# Display a message indicating successful setup
echo "Virtual environment $VENV_NAME is created, activated, and requirements are installed."

# Run python application.
python3 app.py

This script create a python virtual environment and installs Flask as a dependency in the env. Python virtual environments are a great way to keep your root python installation clean from unnecessary dependencies. In addition that, it also helps you to test your application in an isolated environment.
Run this script with command sh +x test-web-app.sh

  1. Time to see your application in action. Click http://127.0.0.1:5000/

You should see Hello World on your Browser Window.

Congratulations ! You just created a simple Web application in python.
Now is the time to give yourself the first pat on the back for completing the first milestone.

By the end of this, your directory structure would look something like:

hello-world-to-kubernetes
└── src
    ├── app.py
    ├── my_py_web_app
    ├── requirements.txt
    └── test-we-app.sh

Dockerize our "hello world" application.

Let's go on to Dockerize this application.
Just before we do that let's set up some context about Docker.

Why use Docker ?

  1. Docker will enable us to run our application in a container.
    A container can be thought to be a limited environment for our web application containing only our application code and its dependencies.

  2. We will package our application into a Docker image.
    A docker image is a file with set of instructions to package our application.

With that let's step into creating the Docker file.

  1. Create a file named Dockerfile in dir hello-world-to-kubernetes
# Use an official Python runtime as a parent image
FROM python:3.8-slim

# Set the working directory to /app
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY ./src/ /app

# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# Make port 5000 available to the world outside this container
EXPOSE 5000

# Run app.py when the container launches
CMD ["python", "app.py"]
  1. Let's build the docker image.

    • While in directory hello-world-to-kubernetes run command docker build . -t hello-world:0.0.1 . Wait for it to complete.

    • docker build . -t hello-world:0.0.1 - Builds a docker image with Dockerfile in current dir - . and tag -t it with name hello-world:0.0.1

  2. Let's run the docker image as Docker container.

    • Run command docker run -p 5000:5000 hello-world:0.0.1

    • This command run the image hello-world:0.0.1 we created in step 2.

    • This command also port forwards all requests from our local machine port 5000 in running docker container port 5000.

  3. Let's see it running. Click http://127.0.0.1:5000/

Congratulations, you now have your web application running out of a real docker container. This would be that second pat on the back!

By the end of this your dir structure should look like

hello-world-to-kubernetes
├── Dockerfile
└── src
    ├── app.py
    ├── my_py_web_app
    ├── requirements.txt
    └── test-we-app.sh

Where to from here ?

Now that we have learnt creating a Docker image for a simple python application, we should think about deploying this image in Kubernetes.
Follow along in Part 2