Cannot GET request to flask app localhost in Docker container

Cannot GET request to flask app localhost in Docker container

Problem Description:

i have simple app packed in container

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")

The container with app running successfully

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

When i try

 curl 0.0.0.0:8080/readiness

I got

curl: (52) Empty reply from server

when i use requests

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

I got requests.exceptions.ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))

How can request flask app in Docker properly ?

Solution – 1

You need to publish the 8080 port.
Currently, you have 8080/tcp, 0.0.0.0:8080->80/tcp but this configuration is the 8080 port inside the container is published on port 80 from local machine.

You need 8080/tcp, 0.0.0.0:8080->8080/tcp (and same with 8090), add -p 8080:8080 to docker run command and check with docker ps.

Another detail, you can use localhost or 127.0.0.1 for get, 0.0.0.0 is not recommended.

Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.
Accept
Reject