Python + FastAPI + PostgreSQL + Redis
FastAPI is a modern framework for building APIs in Python. It validates input and output data using type annotations, automatically generates interactive OpenAPI documentation, and runs on ASGI, so it natively supports asynchronous code.
This Stack includes PostgreSQL and Redis: the typical combination for an API that persists data and needs caching or background tasks.
Services included
In addition to your application, the Stack brings up PostgreSQL, Redis, an nginx that receives traffic on port 80, an automatic backups service, and the web file manager.
Project structure
.
βββ app
β βββ main.py
β βββ requirements.txt
βββ backup
β βββ backup-runner.sh
βββ nginx
β βββ default.conf
βββ .env.example
βββ app.Dockerfile
βββ compose.yaml
Getting started
Install the Stack and start it:
docker-deploy-stack --stack python/fastapi-postgres-redis
cd /root/stack/python/fastapi-postgres-redis
docker compose up -d
Then replace the example application with yours, from Git, via SFTP, or from the web file manager, following how-to-deploy-an-application-on-a-stack.md. Your code goes in the app/ folder.
Each time you update the code, apply the changes:
docker compose up -d --build
Configuration
Editable values are in the Stackβs .env file:
| Variable | Default | What itβs for |
|---|---|---|
APP_PORT |
80 |
Public port of the Stack. |
APP_ENV |
development |
Application environment. |
DB_NAME |
fastapi |
Database name. |
DB_USER |
fastapi |
Database user. |
BACKUP_ENABLED |
1 |
Automatic backups: 1 enabled, 0 disabled. |
BACKUP_INTERVAL_SECONDS |
86400 |
How many seconds between backups. |
BACKUP_RETENTION_DAYS |
7 |
How many days backups are kept. |
POSTGRES_VERSION |
16-alpine |
Corresponding image version. |
REDIS_VERSION |
7-alpine |
Corresponding image version. |
After changing it, apply the changes with docker compose up -d.
Useful commands
docker compose ps # see the containers that are running
docker compose logs -f # view live logs
docker compose restart # restart the services
docker compose down # stop and remove the containers