/docker-nginx-uwsgi-flask-mssql

Docker image for Python Flask Apps with official Microsoft SQL Driver, NGINX and uWSGI

Primary LanguageDockerfileApache License 2.0Apache-2.0

Supported tags and Dockerfile links

You must explicitly use one of the tags above. The latest tag is not assigned since each tag represents a different variant, not an incremental version.

NGINX-UWSGI-FLASK-MSSQL

Docker image for Python Apps with Nginx, uWSGI, Flask, MS SQL Driver and pyodbc running in a single container to enable Python Flask Web Apps that require MS SQL connectivity and scaling for production with Nginx.

GitHub Repo: https://github.com/robpco/docker-nginx-uwsgi-flask-mssql

Docker Hub Images: https://hub.docker.com/r/robpco/nginx-uwsgi-flask-mssql/

Overview

This Docker image allow the creation of Python Web Apps with MS SQL connectivity to run on Nginx via uWSGI. It simplifies the task of using Flask Web Apps with MS SQL and serves web requests via Nginx which is recommended for production deployment. These images use pythonx.x-stretch images as the base, so additional packages are installed from Debian stretch.

  • This image is similar to my nginx-uwsgi-flask image but includes the official Microsoft SQL Driver, unixODBC Driver and pyodbc library.
  • Installing the official Microsoft SQL Driver in a container can be tricky as it requires installation from a distribution specific Microsoft repository, and installation and special configuration of locales.

This repo auto-generates images to Docker-Hub. It currently supports Python 2.7, 3.6 and 3.7.

Usage

Basic usage information is provided below.

Run a local application using the container

This is useful during development as you can edit the code on your local machine.

docker run --name webapp -d -p 8080:80 -v ./app:/app robpco/nginx-uwsgi-flask-mssql:python2.7
  • The name of your application should be main.py
  • The folder containing it must be mapped to /app folder in the container.
  • The default filename can be changed by creating a custom uwsgi.ini file
    • place it into the folder containing the application.

Create an image for your Flask Web-App using this image as a base

In this example, the app directory contains the application, named main.py, and any python library requirements are stated inside the requirements.txt file included in the app directory.

FROM robpco/nginx-uwsgi-flask-mssql:python2.7

COPY ./app /app
WORKDIR /app

RUN pip install -r requirements.txt

Custom Environment Variables

This image can be customized by setting the following custom environment variables:

The variables that begin with STATIC_ allow configuring Nginx to relay "static content" directly without going through uWSGI or Flask. This is advantageous for basic HTML pages, css and js files, that don't need their output adjusted by your Flask App.

  • STATIC_INDEX - serve '/' directly from /app/static/index.html
    • 0 = disabled (default)
    • 1 = enabled - the file index.html located in the /app/static directory (in the container) will be forwarded to any requests to the root of your server (/) will
  • STATIC_URL - external URL where requests for static files originate
  • STATIC_PATH - container location of static files (absolute path)
  • UWSGI_INI - the path and file of the configuration info
    • default: /app/uwsgi.ini
  • NGINX_MAX_UPLOAD - the maximum file upload size allowed by Nginx
    • 0 = unlimited (image default)
    • 1m = normal Nginx default
  • LISTEN_PORT - custom port that Nginx should listen on
    • 80 = Nginx default

Setting Environment Variables

Environment variables can be set in multiple ways. The following examples, demonstrate setting the LISTEN_PORT environment variable via three different methods. These methods apply to the other Environment Variables as well.

Setting in a Dockerfile

# ... (snip) ...
ENV LISTEN_PORT 8080
# ... (snip) ...

Setting during docker run with the -e option

docker run -e LISTEN_PORT=8080 -p 8080:8080 myimage

Setting in docker-compose file using the environment: keyword in a docker-compose file

version: '2.2'
services:
  web:
    image: myapp
  environment:
    LISTEN_PORT: 8080