简体   繁体   English

无法从我在 docker 容器下运行的应用程序连接到 localhost:8000 的快速 api 服务器

[英]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.我是使用 fastapi 的新手。 I have a main.py inside docker container.When I connect to fastapi using我在 docker 容器中有一个 main.py。当我使用连接到 fastapi 时

uvicorn main:app —-reload 

from my container.I am prompted to connect to http://127.0.0.1:8000 .从我的容器中。系统提示我连接到http://127.0.0.1:8000 On copying the address to Firefox I am getting the error:将地址复制到 Firefox 时出现错误:

 unable to connect. 

How can I connect to fastapi server ?如何连接到 fastapi 服务器?

PS 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 PS 我正在使用的 git 分支是由另一位同事开发的,所以我几乎不知道 fastapi 是如何在 docker 中设置的

You need to use the command你需要使用命令

uvicorn main:app --reload --host 0.0.0.0

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.通过部署应用程序将具有公共地址和 webdriver.Remote 可以使用它,在 dev 中可以在本地运行 selenium。

test_my_web.py 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法从外部连接到运行CherryPy服务器的docker容器 - Cannot connect externally to a docker container running CherryPy server 如何从其他机器访问服务器(在docker容器中运行)? - How to access a server(which is running in a docker container) from other machine? 无法在 docker 容器中使用 gunicorn 连接到烧瓶应用程序 - Cannot connect to flask application with gunicorn in a docker container 在我的终端服务器 (localhost:8000) 中记录一条消息 - Log a message in my terminal server (localhost:8000) 无法从我的 docker 容器连接到远程数据库实例,但是可以从我的主机连接 - Cannot connect to remote Database instance from my docker container, however can connect from my host computer 从主机连接到运行MongoDB的Docker容器 - Connect from host to Docker container running MongoDB 从 docker 容器连接主机上运行的服务 - connect with service running on host from docker container 在本地计算机上运行的Django无法连接到运行MYSQL的Docker容器 - django running on local machine cannot connect to docker container running MYSQL Ansible 可以从 localhost 连接到 windows 机器,但不能从 docker 容器连接 - Ansible can connect to windows machine from localhost but not from docker container 在本地主机上运行 docker 容器不起作用 - Running a docker container on localhost not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM