Skip to content
This repository has been archived by the owner on Jul 24, 2022. It is now read-only.

yaojiach/docker-flask

Repository files navigation

Docker Flask Boilerplate

Tested on Mac, Linux, Windows

Docker + Flask + JWT boilerplate with gunicorn, nginx, and external (dockerized for dev) Postgres database. Good for quickly set up an authentication backend (for example for a frontend development). Implemented user registration, user login, access token, refresh token, and token revocation.

  • Use Pipfile for dependency management.
  • Use Flask-Restful as the REST API framework.
  • Use Flask-JWT-Extended as the (opinionated) JWT framework. Including features like refresh token and token revoking.

Features

  • Docker
  • nginx
  • gunicorn
  • flask
  • jwt
  • Postgres
  • redis (Used to store jwt token information)

Non-Features

  • external Postgres
  • Dockerized Postgres for dev

Usage

Dev with dockerized Postgres

docker-compose --file docker-compose-dev.yml up --build

Stand up external Postgres database

bash db/init.sh

Build containers

docker-compose up --build

Full clean up (remove Postgres volume)

docker stop $(docker ps -a -q)
docker-compose rm -fs
docker system prune
rm -rf postgres_data

User Registration example

{
    "email": "test@test.com",
    "password": "12345"
}

Example in Postman:

Registration Example

Gotchas

Set PROPAGATE_EXCEPTIONS to propagate exceptions from flask-jwt-extended

class Config:
    ...
    PROPAGATE_EXCEPTIONS = True

Must include Pipfile.lock for pipenv to install system-wide in docker

...
COPY Pipfile.lock /home/project/web
...

Use host.docker.internal inside container to access host machine's localhost

DATABASE_URL=postgresql://dev:12345@host.docker.internal:5432/jwt

Caveats

  • Should use external redis for production

References