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

We Are Going To Discuss About Cannot connect to fast api server at localhost:8000 from my application which is running under a docker container. So lets Start this Python Article.

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

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

    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.

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

    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.

Solution 1

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.

Original Author lsabi Of This Content

Solution 2

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)

Original Author Evgene Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

ittutorial team

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment