通过容器IP访问本地Docker容器服务器失败

问题描述 投票:0回答:0

我在本地通过Docker运行了一个Python Sanic HTTP服务器,我可以在我的浏览器上通过

localhost:8000
127.0.0.1:8000
访问它:

$ docker run --rm \
    -p 8000:8000 \
    -v ./app:/app \
    --name my_server \
    my_server

[2023-05-09 15:50:29 +0000] [1] [INFO] Sanic v23.3.0
[2023-05-09 15:50:29 +0000] [1] [INFO] Goin' Fast @ http://0.0.0.0:8000
[2023-05-09 15:50:29 +0000] [1] [INFO] mode: debug, single worker
[2023-05-09 15:50:29 +0000] [1] [INFO] server: sanic, HTTP/1.1
[2023-05-09 15:50:29 +0000] [1] [INFO] python: 3.11.3
[2023-05-09 15:50:29 +0000] [1] [INFO] platform: Linux-5.15.49-linuxkit-aarch64-with-glibc2.31
[2023-05-09 15:50:29 +0000] [1] [INFO] auto-reload: enabled
[2023-05-09 15:50:29 +0000] [1] [INFO] packages: sanic-routing==22.8.0
[2023-05-09 15:50:29 +0000] [1] [DEBUG] Creating multiprocessing context using 'spawn'

现在找到容器IP:

$ docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' my_server
172.17.0.2

我尝试通过这个IP和端口访问服务器(

172.17.0.2:8000
),但是浏览器一直pending,没有任何反应。

是否可以在浏览器上通过容器IP访问网页?


Sanic 应用程序简单演示:

from sanic import Sanic
from sanic import response

app = Sanic("my-hello-world-app")

@app.route('/')
async def test(request):
    return response.text("Hello World!!")

if __name__ == '__main__':
    app.run(
        host='0.0.0.0',
        port=8000,
        dev=True,
        debug=False
        )
docker ip sanic
© www.soinside.com 2019 - 2024. All rights reserved.