Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot connect to fast api server at localhost:8000 from my application which is running under a docker container

I a newbie to working with fastapi. I have a main.py inside docker container.When I connect to fastapi using

uvicorn main:app —-reload 

from my container.I am prompted to connect to http://127.0.0.1:8000. On copying the address to Firefox I am getting the error:

 unable to connect. 

How can I connect to fastapi server ?

P.S The git branch I am working from was developed by another colleague so I have little to no idea how was fastapi was set up inside the docker

like image 554
Shafia Askari Avatar asked Jan 21 '26 01:01

Shafia Askari


1 Answers

You need to use the command

uvicorn main:app --reload --host 0.0.0.0

Your docker container is like a computer, which is independent. Thus it does not allow access from external sources. With the --host option, you allow external connections (outside of localhost from the point of view of the container). Basically, docker's localhost is different from your computer's localhost.

like image 195
lsabi Avatar answered Jan 23 '26 20:01

lsabi