Tue. Mar 28th, 2023

This tutorial will explain the step by step procedure on building a basic Flask application. Flask can be defined as a lightweight WSGI web application framework. It’s quick and easy to get started and is designed that way along with the ability to scale up to complex applications. 

Let’s start with building a simple basic Flask application first. Later we will work on dockerizing this application using the containerization concept using Docker and Docker Hub.

Let us start this application in a separate virtual environment first so that the dependencies are good only for this application and this won’t disturb your other applications dependencies and versions. 

Assuming that I will place this application on my desktop folder, I will move to the desktop directory and then create a folder for this application.

mkdir myflaskapp

Now move this directory, 

cd myflaskapp

Install virtualenv package to create virtual environments,
pip install virtualenv

(This installs virtual environments package)

then run the below command to create a virtual environment for this application.

virtualenv flask_env

(here “flask_env” is the name of the virtual environment, so feel free to put your own desired name)

Type the below to confirm that the virtualenv is created


(you should see the flask_env folder there which confirms that the virtualenv is created)

Once you created the virtual environment, you can activate it by using,

source flask_env/bin/activate

Once it’s activated you should see the environment name (flask_env) on your screen.

To deactivate the env, enter command “deactivate” in the command line and this should remove the (flask_env) from your screen which means that it is deactivated.

If you want to see what packages are installed on this virtual environment, then type

pip list

Time to install flask now. Install by typing 

pip install flask

This should install flask for you and you can get the version of flask and python by typing 

Flask -- version 
Python --version

Once Flask is installed, check the list of packages and versions in your virtual environment by typing

pip list
(flask_env) A-Mac:myflaskapp rsa$ pip list
Package      Version
------------ -------
click        7.1.1  
Flask        1.1.1  
itsdangerous 1.1.0  
Jinja2       2.11.1 
MarkupSafe   1.1.1  
pip          20.0.2 
setuptools   46.1.3 
Werkzeug     1.0.1  
wheel        0.34.2 

Now lets create a file called “flaskapp.py” in the myflaskapp directory and then type this basic code to test run the application

from flask import Flask           # importing flask
flaskapp = Flask(__name__)             # creating flaskapp instance
@flaskapp.route("/")                   # defining end point /
def hello_world():                      # calling method hello_world
    return "Hello World! Welcome to my Flask app. Let's dockerize!"   # returns the content in " "
if __name__ == "__main__":        # for running python flaskapp.py
    flaskapp.run(host='',port=5000)            # run the flask app / you can also specify desired port number here

Make this file executable by tying the below from the myflaskapp directory

sudo chmod +x flaskapp.py

Now it’s time to run our flask application. Enter this in terminal/command line

python3 flaskapp.py

You should see the app running on your localhost on port 5000. Port 5000 is the default port number of Flask applications. 

Go to your browser and type to open your application

That’s it! It’s pretty simple and straight forward. The Flask application is built. You can add HTML , CSS and other web development technologies to make your Flask application better and there is no end to this. This is just a starting step to build application. 

Here’s the myflaskapp repository on Github


Dockerzing the Flask application will be posted in the next article. If you are interested in dockerizing this application which we build follow the trailing post. You can follow DevOpsPal social media for latest updates.