Using Docker Compose to deploy a lightweight Python REST API with a job queue
Docker is a great platform for deploying large scale distributed enterprise applications, and the same benefits can be applied for simple use cases. However, even at small scale there is often a need to coordinate multiple containers, which is where Docker Compose comes in.
A common example is standing up a REST API with a job queue backend to handle longer running tasks. For this demo Python project, two technologies will be chosen with an eye towards simplicity and speed.
FastAPI — Alternative to Flask, Django
FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.6+ based on standard Python type hints.
RQ — Alternative to Celery
RQ (Redis Queue) is a simple Python library for queueing jobs and processing them in the background with workers. It is backed by Redis and it is designed to have a low barrier to entry. It should be integrated in your web stack easily.
REST API
We will start by building the base Docker container with the API functionality, and for the ASGI server we will use uvicorn.
Create a new project structure like so, with two empty files: