Boilerplate code for setting up Nginx + Gunicorn + Flask + LetsEncrypt certificates using docker-compose.
This repository contains necessary files to build a web-app running with Nginx / Gunicorn / Flask / LetsEncrypt (Certbot) using Docker and docker-compose.
Note: Tested on Ubuntu 16.04, 18.04, 20.04
service | image |
---|---|
flask & gunicorn | python:3.8-alpine |
nginx | nginx:1.19-alpine |
dependency | commands |
---|---|
docker | commands for Debian / Ubuntu |
docker-compose | commands for Debian / Ubuntu |
make | sudo apt install make |
a domain or sub-domain | DNS A record needs to points to your server static IP |
open ports | 80 (http) and 443 (https) |
sudo git clone https://github.com/smallwat3r/docker-nginx-gunicorn-flask-letsencrypt.git
Install docker, docker-compose and make (commands can be found above).
sudo usermod -aG docker $USER
Log out from the server and log back in for changes to apply.
Copy .env.example
to .env
and set up the environment variables.
# .env
# Email to get alerts from LetsEncrypt.
[email protected]
# Domain name or subdomain linked to your server's public IP.
DOMAIN=mydomain.com
# Folder where your flask app is located in the repo. In this example
# it's under ./src/
APP_FOLDER=src
# Application environment name
FLASK_ENV=development
# It can be the application entrypoint (wsgi if using ./src/wsgi.py)
# or the application package (as in this case) as the app's
# configs are under ./src/example_app/__init__.py
FLASK_APP=example_app
If you're running your own application and not the example one from
this repository you probably need to update or replace the src/Dockerfile
to
your needs (in terms of dependencies etc). It's also known that some Python
projects can have a hard time running from Python's Alpine images, so you might
want/need to switch to a more standard python:3.8
image.
Start application
sudo make dc-start
🎉 Your web-app should now be running online with HTTPS 🎉
All commands
% make help
Usage: make [TARGET ...]
help Show this help menu
dc-stop Stop docker (might need sudo)
dc-start Start docker (might need sudo)
dc-start-local Start docker for local dev (w/o nginx)
Auto checks are running weekly to update the certificates.
See LICENSE file.
Please report issues or questions here.