How to Run Gunicorn

How to Run Gunicorn

Overview

This article will explain how to run Gunicorn (1) to deploy a Flask application.

What is Gunicorn

Gunicorn is a Python WSGI HTTP Server for UNIX. It is compatible with many web frameworks. Gunicorn only runs on UNIX or Linux, therefore for other users, a Docker Linux container can be used to run an application with Gunicorn.

Materials

  • Computer

  • Python (2)

    • Required Python Packages: pip (3), gunicorn, flask
  • Powershell, Command Prompt or Terminal

  • Docker (4) - For users who are not using Linux

To read any documentation or download any needed technologies, refer to "Sources" for more information.

Procedure

For Non-Linux Users, first read "Make a Linux Container to run Gunicorn" to properly set up the needed environment. Afterward, read the "Setting Up the Python Flask File" and "Running Gunicorn" sections.

Setting Up the Python Flask File

To install Flask, run:

pip install flask

Below is the contents of the Python flask file:

from flask import Flask

app = Flask(__name__)

@app.route("/")
def index():
    return "Hello World"

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=8080)

The application will run on port 8080. It can be specified to run on any port. Make sure the Gunicorn command also uses the same port that the Flask file is being run on.

Running Gunicorn

To install Gunicorn, run:

pip install gunicorn

Below is the command to run Gunicorn alongside the Flask application:

gunicorn --bind 0.0.0.0:8080 name_of_flask_file:app

"gunicorn --bind" means it is being attached to a URL that the application can be reached. In this case, the many zeroes or 0.0.0.0 means localhost, and the 8080 is the port number. The application can be reached locally at "localhost:8080". Lastly, the name of the flask_file followed by ":app" must be specified for Gunicorn to know, what Python files are being run.

To access the application, run the command above and visit localhost:8080:

Make a Linux Container to run Gunicorn

To create a Docker Linux container that runs Gunicorn and Flask, use the following Dockerfile below:

Dockerfile

# Run Python
FROM ubuntu:latest

# Update and Upgrade Packages
RUN apt-get -y update
RUN apt-get -y upgrade

# Install nano, python, pip
RUN apt-get install -y nano
RUN apt-get install -y python-is-python3
RUN apt-get install -y python3-pip

# Install Flask
RUN pip install flask

# Install gunicorn
RUN pip install gunicorn

The contents above must be saved in a file called "Dockerfile" and this file has no file extension.

How to Run the Dockerfile

The user must be in the same directory or specify the directory where the Dockerfile is to execute it. Below are the steps to first run the Dockerfile to build an image, and then run the image to make a container:

Building the Image from the Dockerfile

docker build -t give_image_name .

The dot "." executes the current Dockerfile if present in the same directory. Instead of using the dot, the directory specifying where the Dockerfile is can be used if the user is not in the same directory as the Dockerfile.

Running the Image and Making a Container

 docker run -d -p 8080:8080 --name=give_a_container_name -it name_of_container_image

For Docker, the "-d" and "-p" parameters specify the port to access the application when the Docker container is running. In this tutorial, Gunicorn will be deployed to port 8080. This port number must match the same number that is used in the Gunicorn command to deploy the application.

Entering and Leaving the Container

When some images are successfully created, the user enters the newly created container immediately. This is shown if the user is now a root to signify they are in the container. There are also separate commands to enter the container. First, start the container and then execute to enter into the running container:

docker start name_of_container
docker exec -it name_of_container /bin/bash

To leave the container type "exit":

exit

Source Code

https://github.com/AndrewDass1/TUTORIALS/tree/main/Python/Gunicorn/Running%20Gunicorn%20in%20Flask

Sources

https://gunicorn.org/ (1)

https://www.python.org/ (2)

https://pip.pypa.io/en/stable/ (3)

https://www.docker.com/ (4)