Flask + uWSGI + Docker

Flask

Install Flask.

$ pip install flask

Make an application.

$ vi hello.py
from flask import Flask
app = Flask(__name__)

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

Test application.

$ FLASK_APP=hello.py flask run --port 5555

Check the result.

uWSGI

Create a uWSGI configuration file.

$ vi uwsgi.ini
[uwsgi]
module = hello:app
uid = www-data
gid = www-data
master = true
processes = 1

http = 0.0.0.0:5000
chmod-sock = 664
vacuum = true

die-on-term = true

Run uWSGI.

$ uwsgi --ini uwsgi.ini

Check the result.

Docker

Make a file to list the required modules to install in the docker container.

$ vi requirements.txt
Flask==1.0.2
uWSGI==2.0.17.1

Create Dockerfile.

FROM python:3.6-slim

COPY ./hello.py /app/hello.py
COPY ./uwsgi.ini /app/uwsgi.ini
COPY ./requirements.txt /app/requirements.txt

WORKDIR /app

RUN apt-get clean \
    && apt-get -y update

RUN apt-get -y install python3-dev \
    && apt-get -y install build-essential

RUN pip install -r requirements.txt --src /usr/local/src

CMD ["uwsgi", "--ini", "uwsgi.ini"]

Build Docker image

$ docker build . -t app_image

Run Docker

$ docker run --name app_container -p 80:5000 app_image

Check the result

References: