deploymentfastapiporthostingfly

How to tell FastAPI which host and port to bind to


I am trying to deploy my FastAPI backend on Fly.io, but am running into issues because my app is not listening on the 'expected address' which causes slowdowns from Fly.io. Here are their listening port docs.

I would like to configure my FastAPI backend to run on the expected host + port, specifically 0.0.0.0:8000.

How can I do this? I don't invoke the server startup manually with uvicorn, as many older posts for FastAPI suggest. Do I need to add an arg to the app = FastAPI() line in my main script?


Solution

  • I work at Render, not Fly, where we typically use a port number like 10000.

    Usually with FastAPI you'd use a WSGI server process like uvicorn or gunicorn, and those will generally have a --host and --port argument. gunicorn combines them into a -b parameter for binding to an IP/port.

    You could try something like this, assuming your file is main.py and your FastAPI process in there is called app:

    uvicorn main:app --host 0.0.0.0 --port 10000 or gunicorn -b 0.0.0.0:10000 main:app

    0.0.0.0 means to bind to all available IP addresses, otherwise they may only bind to 127.0.0.1 on the hosting provider.