Sample for building a web application with Python3, Flask, Gunicorn, Nginx (HTTPS) and Docker.
Flask's built-in server is not suitable for production. Therefore, we deploy our Flask application to Gunicorn (WSGI server). Gunicorn is best used behind a proxy. In our case, we use NGINX as a reverse proxy. Docker containers make it easy to deploy this architecture on a host system.
First install Docker on your system.
If you are on Linux, add your user to the UNIX group called docker
. For more details, check out Docker post-installation steps. Reboot your system to apply changes:
sudo usermod -aG docker $USER
sudo reboot
Now generate a self-signed SSL certificate (cert.pem, key.pem):
openssl req -x509 -newkey rsa:4096 -nodes -out cert.pem -keyout key.pem -days 365
By default, the Nginx server in this project wants a SSL certificate stored in /var/ssl
on your local machine. You can move your generated cert.pem
and key.pem
file there.
If you prefer to use a different directory (for example, because you are on Windows), you can change it. Simply open the file .env
and replace /var/ssl with your preferred directory.
Now open a terminal in your project directory and launch your web application:
docker compose up
Open in web browser: https://127.0.0.1