Containerization of a flask application

Posted by

Flask Containerization

Introduction to Flask Containerization:

Hello Coders, in this article we shall explore process to containerize a flask application. This is important because the process of containerization is more or less same for other applications.

Process of Conterization(theory):

At high level, first we shall build a flask application. Then we shall try to contanerize the same application using docker file and docker engine.

However, there is a similar article in this my blog, where I have first created a container and build a python application in the container.

Therefore, we can assume that, here we are trying to achieve the reverse of the previous article.

Softwares that are required for this process : docker engine, python, flask. Thefore, I shall assume our machine all software installed.

Note: keep all the files in the same folder. Like main.py and dockerfile.

Creating virtual environment :

$python -m venv envX
# activating the environment
$source envX/bin/activate

The Flask Application(main.py) :

from flask import Flask

app = Flask(__name__)

@app.route('/')

@app.route('/home')

def home():

    return'From flask app home page'



if __name__ == "__main__":

app.run(host="0.0.0.0", port=5000, debug=True)

Two Command to run a flask application:

We can use either of the one, both does almost the same job:

  • Using python command -> python main.py run –host=0.0.0.0:5000
  • Using flask command -> flask –app main run –host=0.0.0.0 –port=5000

Port already in use error :

While developement of a flask project, Python interpreter may show us an error, “Address already in use Port 5000 is in use by another program!”. Therefore, I did a little research to solve this problem!

Basically we have to kill the python process which in running in the background!

  1. Extract the process number:

$netstat -tulnp | grep 5000

or
$lsof -i :5000

2.  Kill the process:

$kill -9 XXXX

Configuring ufw to allow tcp traffic from flask host machine(201) to any pc under the same network :

#ufw allow from 192.168.1.0/24 to 192.168.1.201 port 5000

#ufw reload

Making requirement.txt:

$pip freeze > requirements.txt

Content of the docker file for Containerization :

Create a docker file in the same folder and name it as FlaskDockerFile. Paste the code below in the docker file.

# Use an official Python runtime as a parent image
# Use an official Python runtime as a parent image

FROM python:3.10-slim-buster

# Set the working directory in the container to /app

WORKDIR /app

# Copy the current directory contents into the container at /app

COPY . /app

# Install any dependencies specified in requirements.txt

# Use --no-cache-dir to optimize image size

RUN pip install --no-cache-dir -r requirements.txt

# Make port 5000 available to the world outside this container

EXPOSE 5000

CMD ["flask", "--app", "main", "run", "--host=0.0.0.0", "--port=5000"]

Creating image using the docker file:

$docker build -f FlaskDockerFile . -t myflaskimage:latest

Starting docker container:

docker run -it -d --name flaskapp1 --privileged -p 5001:5000 myflaskimage

Configuring UFW:

We need to configure ufw in such a way that it shoud allow network traffic. Therefore, a request from outside the docker host machine(192.168.1.201) must reach to docker container, in such a way that any IP under 192.168.1.0/24 can access the flask application.

Look at the network diagram below for better understanding!

containerization

  • For all ufw related command always be in root user!
  • edit the /etc/ufw/sysctl.conf file and paste the following lines:
net/ipv4/ip_forward=1 
net/ipv6/conf/default/forwarding=1 
net/ipv6/conf/all/forwarding=1
  • Enter the command below to allow flow of packets
#ufw route allow in on enp2s0 from 192.168.1.0/24 out on docker0 to 172.17.0.0/16 port 5000 proto tcp
#ufw reload
  • When we shall use ufw status command then we can observe as show below:

ufw rule containerization

I will shall detailed video on ufw configuration in near future!

Access the webpage:

Open any browser from the any machine under 192.168.1.0/24, and type http://192.168.1.201:5001/home

flask container page
flask container page

Conclusion of Containerization:

Containerization is one of the important concept among the software professional because it will enable them to move their project from one machine to another machine. In containerization, we dnt have to remember the version of library that is being used in the project. Therefore, lot of burden is remvoed from the developer. This also helps in maintaining the project robustly.

To be very honest docker itselft has atleast tens of reasons to use it. Stay tuned with my blogs, I shall keep posting articles related to docker in this webite.

Leave a Reply

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