Pritam Sinha
Pritam Sinha

Reputation: 447

Why docker not running flask app on 0.0.0.0?

I am trying to dockerize my flask app. I see that docker will host on 0.0.0.0. But I am not getting it. My dockerfile:

#!/bin/bash
FROM python:3.8
COPY . /app
WORKDIR /app
RUN pip install -r requirements.txt
EXPOSE 5000
#ENTRYPOINT [ "./main_class.py" ]
#CMD [ "flask", "run", "-h", "0.0.0.0", "-p", "5000" ]
CMD [ "python", "main_class.py", "flask", "run", "-h", "0.0.0.0", "-p", "5000" ]

When I build it , it build successfully. When I run it docker run -p 5000:5000 iris, it ran successfully. It says Running on http://172.17.0.2:5000/ (Press CTRL+C to quit). Moreover when I see 172.17.0.2:5000 in browser, it does not work. But if I use 127.0.0.1:5000 it works. How can I bring 0.0.0.0:5000 ? In main_class.py I am using

if __name__ == '__main__':
    app.run(debug=True, threaded=True, host="0.0.0.0")

When I type docker ps

CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                                       NAMES
90a8a1ee3625   iris      "python main_class.p…"   23 minutes ago   Up 21 minutes   0.0.0.0:5000->5000/tcp, :::5000->5000/tcp   thirsty_lalande

Is there anything wrong I am doing in dockerfile ?

Moreover when I use ENTRYPOINT, it shows me an error standard_init_linux.go:228: exec user process caused: exec format error, thats why I am not using ENTRYPOINT.

Upvotes: 2

Views: 4365

Answers (1)

jwhb
jwhb

Reputation: 546

The flask run -h 0.0.0.0 will tell your flask process to bind to any local IP address, as mentioned in the comments. This will also allow flask to respond to your Docker port-forwarding (5000->5000), i.e. to answer incoming traffic from the Docker bridge.

On the Docker level your effective port-forwarding is 0.0.0.0:5000->5000/tcp as shown by docker ps, so on your host system port 5000 on any local IP address will be forwarded to your container's flask process.

Please mind that 0.0.0.0 isn't a real IP address, but a placeholder to bind to any local IP address.

You can access your flask application on any IP that routes to your host system, port 5000.

Upvotes: 3

Related Questions