2

I want to run a dockerized flask server locally and access any routes that I define in it. Setup to reproduce my problem:

app.py

from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
    return 'Hello World!'

Dockerfile

from python:3.7-alpine

COPY app.py app.py

RUN pip install flask

CMD [ "flask", "run" ]

Building this container works fine. Following the documentation and some in-depth examples, running it with docker run -p 5000:5000 flask should do the trick. It starts the container and looks good:

* Environment: production   
  WARNING: Do not use the development server in a production environment.  
  Use a production WSGI server instead.  
* Debug mode: off  
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

And docker ps shows me that the port mapping also seems to work:

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                    NAMES
0581bf3af0ea        flask               "flask run"         25 seconds ago      Up 24 seconds       0.0.0.0:5000->5000/tcp   nervous_brown

But I only get a 404 when I try to reach the site in my browser.

Funnily enough, running the image with docker run --net=host flask does work, but I'd rather not use it.

I'm really bad at understanding how networks work, what am I doing wrong?

0

1 Answer 1

8

Change the host that flask is running on to '0.0.0.0'. The localhost in the container is local only to the container, not to your computer:

flask run -h 0.0.0.0

This will bind the app to all network interfaces on the container and will be reachable by your machine

Why Is LocalHost Unreachable?

Docker containers are their own little self-contained networks. They have an external interface, eth0, they have an external ip address, they have routing tables, and a localhost. Localhost doesn't map to an external interface, and it's generally bad practice to try to do so.

Let's take a simple container as an example, I'll just run a linux container like so:

docker run -it ubuntu bash

Now I can check out the network details inside that container by running apt-get update && apt-get install net-tools:

ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.2  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:02  txqueuelen 0  (Ethernet)
        RX packets 11286  bytes 16471897 (16.4 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 3421  bytes 189224 (189.2 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo is loopback, or localhost. It's a completely separate interface and is not externally facing, eth0, however, is. You can bind to it, but I wouldn't guarantee that the ip address is the same all the time. So the easiest way is to bind flask to all of them.

Loopback is simply for the network to communicate with itself, nothing more. It doesn't need to have an externally facing component, because by design it's not intended for external communication

3
  • Would you mind explaining why localhost is unreachable? I mean, in the end its just another ip address, right? Or is everything under 127.x.x.x somehow blocked from reaching the outside?
    – Arne
    Commented Apr 10, 2019 at 14:56
  • Sure one second
    – C.Nivs
    Commented Apr 10, 2019 at 14:57
  • Yeah it does, thank you!
    – Arne
    Commented Apr 10, 2019 at 15:15

Not the answer you're looking for? Browse other questions tagged or ask your own question.