I started to learn docker very recently and tried to set up simple “Hello World!” app with Flask. I built container and ran it with ports
-p 5000 option and by default Flask running on http://127.0.0.1:5000/. When I tried to access container from host on 127.0.0.1:5000 it didn’t work (nor localhost:5000, nor 0.0.0.0:5000). Then I changed my flask app to run on 0.0.0.0:5000 and accessed it from host it worked just fine.
So my question is: "Does container only works with with an ip address of 0.0.0.0 and every app I design should only run on that specific ip?
Thank you in advance.
By default, Flask is only accessible from localhost. When you put your app in a container, localhost changes from your host machine (your laptop) to the container. Containers have their own localhosts and your requests are originating from outside the container.
This is analogous to running Flask on your host/laptop and trying to access it from other computers on the wifi network or the public. It would fail similarly.
When you configure Flask to listen to 0.0.0.0 instead of 127.0.0.1, it will not just listen to requests originating from localhost, but from the outside as well.
Hope this clarified things a bit.