• Create File docker-compose.yml
version: '3'
services:
  flask:
    image: webapp-flask
    build:
      context: .
      dockerfile: Dockerfile-flask
    #restart: always
    logging:
      driver: "json-file"
      options:
        max-size: 10m
        max-file: "5"
    volumes:
      - "./:/app"

    environment:
      - MONGO_HOST=192.168.1.xxxx
      - MONGO_USER=xxxx
      - MONGO_PASS=xxxx
      - MONGO_DB=xxxxx
      - MONGO_PORT=27017
      - PRIVATE_KEY=xxxxx
    networks:
      custom_network:
        aliases:
          - flask


  nginx:
    image: webapp-nginx
    build:
      context: .
      dockerfile: Dockerfile-nginx
    #restart: always
    ports:
      - 5000:80
    logging:
      driver: "json-file"
      options:
        max-size: 10m
        max-file: "5"
    depends_on:
      - flask
    networks:
      custom_network:
        aliases:
          - nginx


networks:
  custom_network:
    external:
      name: nginx-network

  • create file Dockerfile-flask
# Dockerfile-flask
# We simply inherit the Python 3 image. This image does
# not particularly care what OS runs underneath
FROM python:3.7
# Set an environment variable with the directory
# where we'll be running the app
ENV APP /app
# Create the directory and instruct Docker to operate
# from there from now on
RUN mkdir $APP
WORKDIR $APP
# Expose the port uWSGI will listen on
EXPOSE 5000
RUN pip install Flask==1.0.2
RUN pip install uWSGI==2.0.17.1
RUN pip install requests
RUN pip install pymongo
RUN pip install passlib
RUN pip install Flask-HTTPAuth
RUN pip install PyJWT
RUN pip install sshtunnel
# We copy the rest of the codebase into the image
COPY . .
# Finally, we run uWSGI with the ini file we
# created earlier
CMD [ "uwsgi", "--ini", "app.ini" ]
  • Create file Dockerfile-nginx
FROM nginx:latest
# Nginx will listen on this port
EXPOSE 80
# Remove the default config file that
# /etc/nginx/nginx.conf includes
RUN rm /etc/nginx/conf.d/default.conf
# We copy the requirements file in order to install
# Python dependencies
COPY app.conf /etc/nginx/conf.d
  • Create file app.conf
server {
client_max_body_size 20M;
proxy_read_timeout 1000s;
proxy_send_timeout 1000s;
uwsgi_read_timeout 1000s;
uwsgi_send_timeout 1000s;
listen 80;
root /usr/share/nginx/html;
location / { try_files $uri @app;}
location @app {
include uwsgi_params;
uwsgi_pass flask:5000;
}
}
  • Create file app.ini

Link config uwsgi

https://www.techatbloomberg.com/blog/configuring-uwsgi-production-deployment/

[uwsgi]
protocol = uwsgi
; This is the name of our Python file
; minus the file extension
module = app
; This is the name of the variable
; in our script that will be called
callable = app
master = true
; Set uWSGI to start up 5 workers
processes = 5
threads = 2
; We use the port 5000 which we will
; then expose on our Dockerfile
socket = 0.0.0.0:5000
vacuum = true
die-on-term = true
harakiri = 600
  • Create app.py
from flask import Flask, g
from stock_model import getData
from flask import json, Response
from flask import request

app = Flask(__name__)
if __name__ == '__main__':
    app.run(host='0.0.0.0',debug=True)