Flask+Mongo+Redis+Nginx with Gunicorn, Docker Deployment. Part 2

Here we are continuing our previous post. In this post we would deploy our flask application with with nginx using gunicorn.

Create on wsgi.py file

Create a wsgi.py file in your root project directory, which will be used by gunicorn.

from app import app as application

if __name__ == "__main__":
    application.run()

Install gunicorn to project

Install gunicorn into your project’s python lib dependencies. You can install it via pip on your local machine and for docker deployment add gunicorn to requirments.txt.

gunicorn

Start flask application with gunicorn

Earlier, in our previous post we had started flask application at 5000 port, but now with gunicorn we will use 8800. You can choose any other port if would like to do.

docker-entrypoint-flask.sh

#!/usr/bin/env bash

echo "Start flask dev server"
gunicorn --bind 0.0.0.0:8800 wsgi:application

Nginx configuration file

Let create a nginx.config file, which we would use to configure nginx. This file will be created at root directory as other files.

server {

    listen 80;


    location / {

        proxy_pass http://flaskapp:8800;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
    }

}

nginx’s default request timeout is 30 second, but you can increase it by adding these configurations. In this configuration am increasing the request size as well.

server {

    listen 80;

    client_max_body_size 50M;
    proxy_read_timeout 200;
    proxy_connect_timeout 200;
    proxy_send_timeout 200;

    location / {

        proxy_pass http://flaskapp:8800;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
    }

}

Create Nginx_Docker file

Create a file Nginx_Docker file, that will be use by docker-composer to install nginx web server. This file will be reside at root directory of the project.

FROM nginx:latest

# remove existing/default nginx config file
RUN rm -f /etc/nginx/conf.d/default.conf
# copy our nginx config file to nginx config dir
COPY nginx.conf /etc/nginx/conf.d

Create .env file for docker-composer.yml

Create a .env environment file that will be use by docker-composer.yml for environment variables. Such as we had mongo level variables. Same information you will find here

# Database variables
DB_ROOT_USER=root
DB_ROOT_PW=root
DB_NAME=my_test_db
DB_USER=my_test_user
DB_PASSWORD=mytestpassword


Update docker-composer.yml

Update few new setting to docker-composer.yml file to create container for our project components. such as nginx, redis, mongo.

version: "3.0"
services:

  flaskapp:
    container_name: "my-flask-app"
    restart: always
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "8800:8800"
    command: ./docker-entrypoint-flask.sh
    depends_on:
      - mongo
      - redis

  redis:
    container_name: "my-redis"
    image: "redis"
    restart: always
    entrypoint: redis-server --appendonly yes

  mongo:
    image: mongo
    container_name: "my-mongo-db"
    restart: always
    environment:
      MONGO_INITDB_ROOT_USERNAME: '${DB_ROOT_USER}'
      MONGO_INITDB_ROOT_PASSWORD: '${DB_ROOT_PW}'
      MONGO_INITDB_DATABASE: '${DB_NAME}'
      MONGO_INITDB_USERNAME: '${DB_USER}'
      MONGO_INITDB_PASSWORD: '${DB_PASSWORD}'
    volumes:
      - ./init-mongo.sh:/docker-entrypoint-initdb.d/init-mongo.sh

  nginx:
    container_name: nginx
    image: nginx
    restart: always
    build:
      context: .
      dockerfile: Nginx_Dockerfile
    ports:
      - "80:80"
    depends_on:
      - flaskapp

That’s it. Let try to build and run the docker containers.

sudo docker-compose build
sudo docker-compose up -d

# check docker status
sudo docker ps

# check logs of container
sudo docker logs my-flask-app -f
sudo docker logs nginx -f

Flask+Mongo+Redis+Nginx with Gunicorn, Docker Deployment. Part 2
Show Buttons
Hide Buttons