April 18, 20247 minutes
Flask is a small and lightweight Python web framework that provides useful tools and features for creating web applications. It’s known for its simplicity and flexibility, making it a popular choice among developers.
Creating a web application with Flask can be quick and straightforward. We can build a web application using only a single Python file. This makes Flask a more accessible framework for new developers.
Flask doesn’t force a particular directory structure or require complicated boilerplate code before getting started. This means we have the freedom to structure project in a way that best suits needs.
One of the key features of Flask is its extensibility. We can add more functionality to Flask application using extensions. This allows to customize application to fit specific requirements.
Flask uses the Jinja template engine to dynamically build HTML pages using familiar Python concepts such as variables, loops, lists, and so on. This means it can create dynamic web pages without having to write a lot of JavaScript.
Before start building a Flask application, we’ll need a local Python 3 programming environment. We’ll also need an understanding of Python 3 concepts, such as data types, conditional statements, for loops, functions, and other such concepts.
Once we’ve set up environment, we can install Flask using the pip package installer. After that, we’re ready to start building Flask application.
nginx:7151
: The Nginx web server listening on port 7151.app1
, app2
, app3
: These sections are represent the Apps that Nginx can forward requests to (it will automatically give each app a unique port, after building the container).The topology of the project consists of client requests coming into the Nginx server, which is listening on port 7151
. The Nginx server can forward these requests to multiple Flask applications (app1
, app2
, app3
), each running in its own Docker container. This setup allows for load balancing and efficient distribution of client requests, ensuring that the system can handle a large number of simultaneous connections.
The Dockerfile
contains app will create python environment, /app
as a working directory, The application’s dependencies are listed in a requirements.txt
file and include Flask and Gunicorn. Gunicorn will binding on port 5000
.
The docker-compose.yml
file is used to define and run the multi-container Docker app. It specifies the services to run configuration, and the relationships between them. Docker Compose is a tool for defining and managing multi-container Docker apps.
This nginx configuration file sets up Nginx to listen on port 80
and forward incoming requests to the Flask application running on port 5000
. Nginx is a high-performance HTTP server and reverse proxy. It’s known for its high performance, stability, rich feature set, simple configuration, and low resource consumption.
In the wsgi.py
contain a Flask application instance is created and a route is defined for the application’s home page. The home page displays a greeting and the ID of the container the application is running in. This is done using the socket.gethostname()
function, which returns the host name of the current system under which the Python interpreter is executed.
The template index.html
file is a Jinja template that is rendered by the Flask application. Jinja is a modern and designer-friendly templating language for Python, modelled after Django’s templates. It is fast, widely used and secure with the optional sandboxed template execution environment.
-d
: This option is used to run the containers in the background (detached mode).--build
: This option is used to build images before starting containers.--scale app=3
: This option is used to start multiple containers for a service. In this case, it starts 3 instances of the app
service. This is useful when we want to scale up application to handle more traffic.The docker ps
command output shows that the Docker Compose project is successfully running four containers. We can see one of these is an Nginx server, which is listening on port 7151
and is ready to receive incoming HTTP requests. The other three are instances of a Flask application, each running in its own container and listening on a unique port. This setup demonstrates a scalable web application architecture, where an Nginx server acts as a reverse proxy, distributing incoming requests to multiple application instances to balance the load and increase the system’s capacity to handle traffic.
We can see in the web display running in the browser on the combined port nginx:7151
, look at the container ID section if every time we refresh the value on the container ID will change this indicates that the combined port nginx:7151
has successfully forwarded from the three ports app, this load balancer will be very useful in minimizing a lot of traffic to prevent the server from going down.
docker compose up
are removed.