Open In App

Load Balancing Flask Application using Nginx and Docker

Last Updated : 30 Mar, 2023
Like Article

Load balancing means efficiently distributing the incoming traffic to different server instances. Nginx is open-source software that can be used to apply load balancing to backend systems. Nginx also can be serve services such as reverse proxy, caching, web server, etc.

Docker is a tool that gives a virtualization solution for a complete software package delivery by packaging it into containers. A container is a component that bundles the application along with its dependencies, libraries, and configuration files together and ships the software ready to be worked on any machine with an easy installation process.

Docker Compose is used to run systems having two or more containers. It configures the interconnection between different containers and executes them all in a single command.

We will first run a simple flask instance through docker. Then we will reconfigure the created instances by adding an Nginx load balancer and make the app scalable.

Part 1: Running Flask instance through Docker

Step 1: First, make sure to install Docker into your system.

Step 2: Create a project directory named ‘load_balance_tutorial‘ and navigate it into

$ mkdir load_balance_tutorial
$ cd load_balance_tutorial

Step 3: Create an ‘app‘ directory and navigate in it

$ mkdir app
$ cd app

Step 4: Create a virtual environment and activate it

$ python3 -m venv venv
$ source venv/bin/activate

Step 5: Install flask

$ pip3 install flask

Step 6: Create a boilerplate application for a flask with the file name ‘

from flask import Flask

app = Flask(__name__)

# Route for the default page
def home():
    # Display message
    return "<center><h3>Welcome to GFG</h3></center>"

if __name__ == '__main__':'')

Step 7: Create a ‘requirements.txt‘ file

$ pip3 freeze > requirements.txt

Step 8: Creating a ‘Dockerfile

# Pick a low configuration python base image
FROM python:alpine

#  Expose the port 5000 of the docker container

# Create a folder app in container and work inside it

COPY requirements.txt .

# Install all the requirements
RUN pip3 install -r requirements.txt

# Copy all the flask project files into the WORKDIR
COPY . .

# Execute flask application inside the container
CMD python3

Step 9: Create a ‘.dockerignore‘ file to ignore the venv folder


Now we have successfully configured our flask application with the docker services. Now the next part is to build the Nginx load balancer.

Part 2: Configure Nginx with Docker

Step 1: Go to the ‘load_balance_tutorial‘ directory

$ cd ..

Step 2: Create a new directory named ‘Nginx‘ and navigate to it

$ mkdir nginx
$ cd nginx

Step 3: Create a configuration file for Nginx named ‘Nginx. conf‘. This configuration file will serve as the main file for the load balancing application. By default, Nginx will use the round-robin method for server allocation to requests.

# events are used to set general configurations on how
# nginx will handle the connection requests
events {}

http {
    # Define the group of servers available
    upstream app {
        server app;
        server load_balance_tutorial_app_1:5000;
        server load_balance_tutorial_app_2:5000;
    server {
        # Server group will respond to port 80
        listen 80;
        location / {
            proxy_pass http://app;

Step 4: Create a new ‘Dockerfile

FROM nginx

# Override the default nginx configuration file
RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/nginx.conf

With this, we have successfully configured the load balancer and have attached it to the docker container. Now in the next part, we will combine the flask application and the Nginx load balancer to run simultaneously using docker-compose.

Part 3: Setting up the docker-compose

Step 1: Go to the load_balance_tutorial directory’

$ cd ..

Step 2: Create a file name ‘docker-compose.yml

Step 3: Edit the docker-compose.yml file as follows

version: '3.7'

  # Build the app services
    build: app
    container_name: nginx
    build: nginx
    # Bind the port 80 of container to machine port 80
      - 80:80
    # Make app as nginx dependency service
      - app

With this, the services ‘app‘ and ‘Nginx‘ have been linked together.

Part 4: Running the system

Step 1: Run the following command to initiate the services. The system will run two instances of the flask application

$ docker-compose up --build -d --scale app=2

Step 2: Check the containers by using the following command:

$ docker ps --format '{{.Image}} {{.Names}}'

The above command must give a similar output as shown below

docker ps –format ‘{{.Image}} {{.Names}}’

Step 3: Go to http://localhost in your browser and see the output.


Step 4: To check whether the two instances are working properly or not, that is whether they are load-balanced or not would check the log files for the flask containers and see for 200 status codes for each refresh in the server for both instances.

$ docker logs load_balance_tutorial_app_1 -f
$ docker logs load_balance_tutorial_app_2 -f

You should be getting a similar type of output.

docker logs load_balance_tutorial_app_1 -f

docker logs load_balance_tutorial_app_2 -f

Finally, the flask application now has two instances and both of them are load balanced using Nginx and Docker.

Similar Reads

How To Use Azure Application Gateway for Web Application Firewall and Load Balancing?
Microsoft Azure offers an application gateway solution that enables users to control inbound traffic to their web apps. For improved security and speed, it provides load balancing capabilities and a web application firewallIn this article, we'll walk you through using Azure Application Gateway for load balancing and web application firewall. Termin
3 min read
Using Nginx As HTTP Load Balancer
Load balancing is a technique used in modern application servers for fault-tolerant systems. The load balancer is a networking device that splits network traffic across multiple backend servers so that the load on each server becomes constant making the overall system persistent and fault-tolerant. Nginx server is an enterprise-grade web server tha
5 min read
Build, Test and Deploy a Flask REST API Application from GitHub using Jenkins Pipeline Running on Docker
Nowadays even for small web applications or microservices, we need an easier and faster way to deploy applications that is reliable and safe. These applications may be simple but they undergo rapid changes from the business requirement, to handle these changes we definitely need a CI/CD pipeline for deployment. Jenkins is one such tool that can mak
4 min read
Docker - Container for NGINX
Docker is an open-source platform that enables developers to easily develop, ship, and run applications. It packages an application along with its dependencies in an isolated virtual container which usually runs on a Linux system and is quite light compared to a virtual machine. The reason is that a container shares the OS kernel with only its bina
11 min read
How to Configure NGINX in Docker Compose?
In modern web development, the act of containerization has acquired widespread reception because of its proficiency, portability, and adaptability benefits, containerization allows developers to package applications and their conditions into lightweight, isolated containers, ensuring consistency across various conditions and working on deployment p
7 min read
How to Dockerize django application for production deployment with Gunicorn and Nginx
Docker is an open-source containerization platform used for building, running, and managing applications in an isolated environment. A container is isolated from another and bundles its software, libraries, and configuration files. Django is an open-source Python web framework that can be used to quickly develop fully functional web applications. I
5 min read
How to create load balancing servers using Node.js ?
If your website or application doesn't receive many requests, you don't have to use load balancing, but when it becomes very popular and starts to receive a high amount of traffic may your underlying server can not handle it. Because a single NodeJS server is not flexible for handling a very large amount of traffic. Adding more machines can solve t
8 min read
AWS Load Balancing Using Terraform
AWS load balancing is a service that splits incoming network traffic across multiple resources. These resources can be EC2 instances, Containers, and IP addresses. AWS provides various types of load balancers. Terraform can be used for easy and hassle-free deployment of AWS load balancers. Let's see how we can deploy the AWS load balancer using Ter
4 min read
Kubernetes Ingress Controllers: Routing And Load Balancing For Services
In the dynamic landscape of containerized applications, efficiently orchestrating and managing services is critical. As an effective platform for container orchestration, Kubernetes offers a stable foundation for launching, growing, and overseeing containerized applications. The Ingress controller is a crucial part of Kubernetes that helps with eff
6 min read
Microsoft Azure - Finding the Right Load Balancing Service
Pre-requisite: AZURE We can divide incoming application traffic among many targets, such as Virtual Machines (VMs), containers, and IP addresses in our VPC, using Azure load balancer. The load balancer can increase the high availability of applications and decrease the latency for end users. Azure load balancer supports TCP and UDP protocols as wel
8 min read