DEV Community

Yogendra Tamang
Yogendra Tamang

Posted on

Deploy Flask App on Kubernetes - 1


This is part one of two part series:
Here, I will discuss about creating simple flask app, containerizing it and pushing it to image repository DockerHub.


Create Flask APP

Lets create requirements.txt files for our app.


Flask comes with one development server, but we will use production-ready server, like gunicorn.

Create src folder and file inside that. Our App will have only two endpoints:

  • / -- returns welcome message.
  • /names -- lists countries I visited :P
from flask import Flask, jsonify
app = Flask(__name__)
from flask import Flask, jsonify
app = Flask(__name__)

country_list = [

def index():
    response_dict = {
        "message": "Welcome to FlaskApp"
    return jsonify(response_dict)

def countries():
    country_dict = {
        'countries': country_list,
        'total': len(country_list)
    return jsonify(country_dict)

if __name__ == '__main__':, host='', port=8083)

Run your app in debug mode

python src/

Run your app from gunicorn

gunicorn --chdir src app:app -w 2 -bind

Lets create, we will execute this directly

#! /bin/sh
gunicorn --chdir src app:app -w 2 -b

(Make it executable: chmod +x

You can browse http://localhost:8083/

Building Docker Image

Lets write Dockerfile to create docker image of our app.
Here I am going with python-3.7-slim-buster.

This is one of light weight and recommended images to use.
Follow this if you want to learn more about other python images.

FROM python:3.7-slim-buster

COPY requirements.txt .

RUN pip install -r requirements.txt

COPY . /app



Now run following command to build image

docker build -t simple-flask-app .
This will take some time depending on your network speed. Once completed you will see success message like this

Run your docker image

docker run -p 8888:8083 simple-flask-app
If you browse http://localhost:8888/names, you will see following output.

Curl Output

Now Push your image to DockerHub using following commands

$ docker tag simple-flask-app <dokcer-hub-account>/simple-flask-app

$ docker push <dokcer-hub-account>/simple-flask-app

If you are not logged in, log in first.

$ docker login

Top comments (0)