Create a Flask+uWSGI+Nginx Docker Image

Create a Flask+uWSGI+Nginx Docker Image

In this example, we will create a Docker image for a Flask application using uWSGI and Nginx. The image will be based on the tiangolo/uwsgi-nginx-flask image, which is a popular choice for deploying Flask applications in production.


Directory Hierarchy

Create a directory for your project. Inside this directory, create a subdirectory for your Flask application and a Dockerfile in the root directory.

1
2
3
4
5
6
7
.
├── app
│ ├── __init__.py
│ ├── main.py
| ├── ...
│ └── uwsgi.ini
└── Dockerfile

Create Dockerfile

Create a Dockerfile in the root directory of your project. This file will define the image for your Flask application.

in this case, because we use opencv-python3, we need to install ffmpeg and libsm6 libxext6 in the image.

1
2
3
4
5
6
7
8
9
10
11
FROM tiangolo/uwsgi-nginx-flask:python3.10

COPY ./requirements.txt /app/requirements.txt
RUN python3 -m pip install --no-cache-dir --upgrade -r /app/requirements.txt

RUN apt update && apt install -y ffmpeg libsm6 libxext6

ENV LISTEN_PORT=8080
EXPOSE 8400

COPY ./app /app

Customized uwsgi.ini

This image will auto create a uwsgi.ini file in the /app directory.
If needed, you can customize the uwsgi.ini file at ./app to replace it when build.

1
2
3
4
5
6
7
[uwsgi]
module = main
callable = app
enable-threads = true
master = 1
processes = 2
buffer-size = 32768

References