docker 无法访问主机中的 gunicorn 服务资源

deo*_*oll 2 python simplehttpserver gunicorn docker

我加载了一个简单的 docker ubuntu 镜像。启动容器。在该容器中安装 curl。

在 docker 主机上。我写了两个基于 python 的 web 服务器。一个基于 SimpleHTTPServer(托管在端口 4000)和一个基于 falcon(托管在 5000 和 gunicorn 上)。

可以从容器外壳访问基于 python 的 Web 服务器:

root@430a51680859:/# curl http://172.17.0.1:4000
<!DOCTYPE>
<html>
    <head>
        <title>Hello World</title>
    </head>
    <body>
        <h1>Welcome</h1>
        <p>Hello World</p>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

但是 gunicorn 只是失败了:

root@430a51680859:/# curl http://172.17.0.1:5000/quote
curl: (7) Failed to connect to 172.17.0.1 port 5000: Connection refused
Run Code Online (Sandbox Code Playgroud)

Xio*_*Jin 5

Gunicorn 默认只监听 localhost (127.0.0.1),而 SimpleHTTPServer 默认监听所有接口。为了能够访问 Gunicorn 服务的页面,请使用-b 0.0.0.0:5000(在所有接口上侦听)或-b 172.17.0.1:5000docker0仅侦听,可从 Docker 容器访问)运行 Gunicorn 。