This project demonstrates a dummy FastAPI app that serves a cat picture, with Nginx configured as a reverse proxy and static file server.
We use Docker Compose v3.9 to orchestrate two containers:
- FastAPI App: Handles API requests and serves templates.
- Nginx: Acts as a reverse proxy for the FastAPI app and serves static files directly. (in front of FastAPI)
- In
docker-compose.yml
we:- Defined two services in
docker-compose.yml
:app
(FastAPI app) andnginx
. - Encapsulated the ports of
app
indocker-compose.yml
- Mounted the
static
folder ofapp
fornginx
.
- Defined two services in
- Created
nginx.conf
file, with using the default image of Nginx.- Mounted the volume of
static
folder ofapp
intonginx
container, as well asnginx.conf
file.
- Mounted the volume of
As FastAPI
doesn't have a command to consolidate static files into a single directory, like Django, here we mounted static
folder twice: once for FastAPI app, then linking it to Nginx's container.
In frameworks with that feature, e.g. Django, you can specify the ROOT_FOLDER
in settings.py
to nginx
folder to output static files straight into nginx
folder, thus mount once and only for nginx container (mounted again).
Instead of wasting time on typing commands, two shell scripts are present.