Deploy Flask App using Nginx and Gunicorn

Not Everyone is able to Deploy Deep Learning App? Here is the Right Solution

Most of the deep learning or machine learning model requires flask for developing an API URL for getting the input and predicting the results. But many newcomers always find it very difficult for deploying their apps. They don’t know the idea behind taking the development phase to the production phase. In this entire tutorial, you will learn how to deploy the flask App using Nginx and Gunicorn using Docker.

Steps by Step Guide to Deploy Flask App

Step 1: Install the Packages

Before creating the app. you have to install all the modules that you want to use for the completion of the project. Here I am using only two packages. flask, gunicorn. Create a text file with the name “requirements.txt” and add all the modules to the file. After go the terminal and write the command to install it.

pip3 install -r requirements.txt

Step 2: Create a Flask App

I know you must have your code to deploy. But for the demonstration purpose, I am only creating the simple flask app that has only print Welcome to the Data Science Leaner message.  Below is the code for it.

from flask import Flask

app = Flask(__name__)

@app.route("/")
def index():
    return "Welcome to the Data Science Learner"

if __name__== "__main__":
    app.run(host="0.0.0.0")

Step 3: Run the app using Gunicorn

Now test the Flask app using the Gunicorn. It will run the app on the localhost: port and gives the output “Welcome to the Data Science Learner”.

gunicorn  "run:app"

Step 4: Create a Docker File

After creating the Flask App now you have to create a Docker File to tell the docker to create an image for the flask app that you want to run. Create the file with the name “Dockerfile” and paste the following code.

FROM python:latest

ENV HOME=/var/www

ADD . $HOME
WORKDIR $HOME

RUN pip3 install -r $HOME/requirements.txt

Step 5: Create a Docker Compose File

Make sure you have created Dockerfile. Now to automate all the above entire process create a  dockercompose.yml file and copy and paste the code give below.

version: '3.1'

services:
  nginx:
    image: nginx:latest
    container_name: nginx
    volumes:
      - ./:/var/www
      - ./default.conf:/etc/nginx/conf.d/
    ports:
      - 81:80
    networks:
      - my-network
    depends_on:
      - flask
  flask:
    build:
      context: ./
      dockerfile: Dockerfile
    container_name: flask
    command: gunicorn --bind 0.0.0.0:8000 --workers 4 "run:app"
    volumes:
      - ./:/var/www
    networks:
      my-network:
        aliases:
          - flask-app

networks:
  my-network:

Step 6: Create the Nginx Configuration File

After creating the docker-compose file now create the Nginx Configuration file with the filename “default.conf” the same name you have created in dockercompose.yml file. The content of the file will be like this.

upstream flask-app {
    server flask:8000;
}

server {
    listen 80;
    server_name localhost;

    location / {
        proxy_pass http://flask-app;
        proxy_set_header Host "localhost";
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_redirect off;
    }
}

The above configuration is proxying all the requests to the port 8000.

After all the steps you have done above run the docker-compose file using the following command and go to your browser and type the localhost you will see the message like the below picture.

welcome to the data science learner flask nginx

Conclusion

There are the basic steps to the Deploy Flask App using Nginx and Gunicorn. If you clearly understand the concept then you will easily deploy your own deep learning or machine learning model on your server. Hope this article helps you if you have any query then you can follow us or contact us for more information.

 

Official Documentation

Nginx 

Docker

Flask

 

 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner