无法GET请求以在Docker容器中创建应用程序localhost

oipij1gg  于 2022-12-11  发布在  Docker
关注(0)|答案(1)|浏览(141)

我有简单的应用程序 Package 在容器中

app = Flask(__name__)

@app.route('/readiness')
@app.route('/liveness')
def health():
    return {"health_status": "running"}, 200

class StandaloneApplication(gunicorn.app.base.BaseApplication):
    def __init__(self, flask_app, gunicorn_options=None):
        self.options = gunicorn_options or {}
        self.gunicorn_app = flask_app
        super().__init__()

    def load_config(self):
        config = {key: value for key, value in self.options.items()
                  if key in self.cfg.settings and value is not None}
        for key, value in config.items():
            self.cfg.set(key.lower(), value)

    def load(self):
        return self.gunicorn_app

if __name__ == "__main__":
    options = {
        'bind': ['0.0.0.0:8080', '0.0.0.0:8090'],
        'workers': 1,
        "timeout": 60
    }
    StandaloneApplication(app, options).run()
    log.info("Server started")

已成功运行应用程序的容器

CONTAINER ID   IMAGE       COMMAND           CREATED              STATUS              PORTS                            NAMES
28a8152aa90f   embs:embs   "python app.py"   About a minute ago   Up About a minute   8080/tcp, 0.0.0.0:8080->80/tcp   determined_galois

[2022-12-06 12:48:59 +0000] [1] [INFO] Starting gunicorn 20.1.0
[2022-12-06 12:48:59 +0000] [1] [INFO] Listening at: http://0.0.0.0:8080,http://0.0.0.0:8090 (1)
[2022-12-06 12:48:59 +0000] [1] [INFO] Using worker: sync
[2022-12-06 12:48:59 +0000] [15] [INFO] Booting worker with pid: 15

当我尝试

curl 0.0.0.0:8080/readiness

我得到了

curl: (52) Empty reply from server

使用请求时

url = '0.0.0.0:8080/readiness'
requests.get(url)

我得到了requests.exceptions.ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))
如何在Docker中正确请求 flask 应用程序?

yhxst69z

yhxst69z1#

您需要发布8080端口。当前,您有8080/tcp, 0.0.0.0:8080->80/tcp,但此配置是the 8080 port inside the container is published on port 80 from local machine
您需要8080/tcp, 0.0.0.0:8080->8080/tcp(与8090相同),将-p 8080:8080添加到docker运行命令,并使用docker ps进行检查。
另一个细节,你可以使用localhost或者127.0.0.1来获取,0.0.0.0不推荐使用www.example.com。

相关问题