Load Balancing Flask Application using Nginx and Docker
Last Updated :
12 Apr, 2025
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 ‘app.py‘
from flask import Flask
app = Flask(__name__)
# Route for the default page
@app.route("/")
def home():
# Display message
return "<center><h3>Welcome to GFG</h3></center>"
if __name__ == '__main__':
app.run('0.0.0.0')
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
EXPOSE 5000
# Create a folder app in container and work inside it
WORKDIR /app
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 app.py
Step 9: Create a ‘.dockerignore‘ file to ignore the venv folder
venv
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;
server_name app.com;
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'
services:
# Build the app services
app:
build: app
nginx:
container_name: nginx
build: nginx
# Bind the port 80 of container to machine port 80
ports:
- 80:80
# Make app as nginx dependency service
depends_on:
- 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
Running a Ruby Application using Docker
Pre-requisite:- Docker Docker is a containerization platform that allows you to package your applications and their dependencies into a lightweight and portable containers. Using Docker, you can easily deploy and run your applications on any platform that supports Docker, without having to worry abo
4 min read
Dockerizing a Python Flask Application with a Database
Applications are packaged, distributed, and run via Docker within Docker containers throughout the process of "dockerizing". The exact environment and dependencies must be installed in order to run that application on another local machine when it is developed under a specific environment and depend
5 min read
Setting Up Flask Applications in Google Cloud Run
Flask is a popular and easy to use Python web framework that is lightweight and flexible. It lets programmers quickly and with minimal code construct web applications. Flask is simple to modify to match the requirements of different project types since it allows you to choose the tools and libraries
5 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 qu
5 min read
How to Run GUI Based Applications inside Docker?
A Docker Container is an isolated application platform that contains everything needed to run an application built from one or more images. Docker is an Open Source project that provides an open platform to run any number of applications inside a container according to your requirements and you can
7 min read
Creating first web application using Bottle Framework - Python
There are many frameworks in python which allow you to create a webpage like bottle, flask, django. In this article, you will learn how to create a simple app using bottle web framework. Bottle is a fast, simple and lightweight WSGI micro web-framework for Python. It is distributed as a single file
2 min read
Joke Application Project Using Django Framework
Django is a high-level Python-based Web Framework that allows rapid development and clean, pragmatic design. It is also called batteries included framework because Django provides built-in features for everything including Django Admin Interface, default database SQLlite3, etc. Today we will create
2 min read
How to Use Docker Images For Scaling Applications?
Docker image is a lightweight piece of software that includes everything that your application needs to run, it includes code, software packages, and libraries. Docker images are built using a Docker file which includes the instruction for creating the image. The Docker image is built on top of a ba
4 min read
Deployment of Angular Application using Github Pages
There are various methods to deploy Angular Application such as Github Pages, Heroku, Firebase, etc. The Github provides the simplest way of all using the Github Pages. Steps to Create and Deploy Sample Angular Application to Github Pages: Install Node.js: a. Windows b. LinuxInstall Angular - CLICre
2 min read
Design First Application Using Express
In NodeJS, the task of designing a server was very difficult, but after the introduction of ExpressJS, which is a lightweight web application framework for NodeJS used to build the back-end of web applications relatively fast and easily. PrerequisitesTo get started with an ExpressJS application, Nod
5 min read