I wrote a dockerfile which used the tiangolo/uvicorn-gunicorn-fastapi as my base image. That image has some smart defaults, but they can be a bit opinionated.

By default, if we don’t override anything, it assumes:

Hence to run the image we do docker run -p 8000:80 -e MODULE_NAME="myapplication.server.main" myapplication. In this case, I am overriding the MODULE_NAME and telling docker that the fastapi app lives in location myapplication.server.main.py

Incase, I did myapp = FastAPI() inside main.py I would run the docker image via docker run -p 8000:80 -e MODULE_NAME="myapplication.server.main" VARIABLE_NAME="myapp" myapplication


On the other hand, if I skip tiangolo defaults and run uvicorn manually, then in my dockerfile I would do CMD ["uvicorn", "myapplication.server.main:myapp", "--host", "0.0.0.0", "--port", "80"] and then run the docker image using docker run -p 8000:8000 myapplication

So we either bake these information into the dockerfile or specify it via the docker run command.