Shafia Askari
Shafia Askari

Reputation: 131

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

Upvotes: 12

Views: 31044

Answers (5)

Vincent Casey
Vincent Casey

Reputation: 690

For my scenario, the port I was attempting to utilize for the application was already busy.

You can find which application is using the port you are trying to use with the following command (for Windows, in admin):

netstat -anob

From here it will list all busy ports and you can determine which application to stop in order to run your FastAPI application at the specified port.

Alternatively, you can run your application on another port. For example:

uvicorn main:app --reload --host 127.0.0.1 --port 5001

Upvotes: 1

Capitan Security
Capitan Security

Reputation: 77

First of all you need to check "is your container up?"

docker ps -a

Then if some pods are not active, you should check the logs of container.

docker logs {container-id}

enter image description here

I changed my dockerfile with your CMD part and ran it. It worked successfully. But I noticed that you are using

docker run --name mycontainer -p 8000:80

Maybe you should try that:

docker run --name mycontainer -p 8000:80 <docker image id>

Upvotes: 1

Tanay Baswa
Tanay Baswa

Reputation: 1

You also need to make sure that your port mapping is correct.

  1. In the Dockerfile:

CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "80"]

The host is set to 0.0.0.0, which is accessible locally as localhost (127.0.0.1 for most).

  1. In the docker commands:
docker build -t myfastapi

docker run --name mycontainer -p 8000:80

This will map your local port 8000 to docker's 80.

So now, you should be able to access your server at localhost:8000

Hope this helped!

Upvotes: 0

Evgene
Evgene

Reputation: 596

A workaround solution. Through on deploy app will have public address and webdriver.Remote can take it, in dev it is ok to run selenium locally.

test_my_web.py

import unittest

from selenium import webdriver
import os

from dotenv import find_dotenv, load_dotenv
from webdriver_manager.chrome import ChromeDriverManager

load_dotenv(find_dotenv())



class TestWebListAll(unittest.TestCase):
    def setUp(self) -> None:
        chrome_options = webdriver.ChromeOptions()
        if os.environ.get("LOCAL_DEV"):  # == 'True'
            self.url = 'http://127.0.0.1:8000/'
            self.driver = webdriver.Chrome(ChromeDriverManager().install())
        else:
            self.driver = webdriver.Remote(
                command_executor='http://localhost:4444',
                options=chrome_options
            )
            self.url = 'insert public addres'  #

    def tearDown(self) -> None:
        self.driver.quit()

    def test_(self):
        self.driver.get(self.url)
        print(self.driver.title)

Upvotes: 0

lsabi
lsabi

Reputation: 4456

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.

Upvotes: 36

Related Questions