How to run Python Flask within a Docker container
<p>I'm trying to run a Python Flask webserver within a docker container, but I can't connect to the Flask server from the outside.  </p>

<p><strong>What I've done:</strong></p>

<p>I created <code>/temp/HelloFlask.py</code></p>

<pre><code>from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

if __name__ == "__main__":
    app.run()
</code></pre>

<p>I started the docker container with container port 5000 mapped to host port 5000 and with ~/temp mounted to /temp</p>

<pre><code>docker run -it -p 5000:5000 -v ~/temp:/temp --name tf gcr.io/tensorflow/tensorflow:latest-devel
</code></pre>

<p>Inside the running docker container, I installed Flask and ran <code>HelloFlask.py</code></p>

<pre><code>cd /temp
pip install Flask
python HelloFlask.py &amp;
</code></pre>

<p>I validated that the server was accessible within the container</p>

<pre><code>[root@de8b6996b540:/temp# curl localhost:5000
127.0.0.1 - - [22/Sep/2016 17:41:48] "GET / HTTP/1.1" 200 -
Hello World!
</code></pre>

<p>I'm using Docker Version 1.12.1 (build: 12133), which exposes the container ports on localhost, so I should be able to access <code>localhost:5000</code> on my Mac, outside the container, but I can't connect.</p>

<p>I tested to make sure that Docker was correctly binding container ports to localhost by running an nginx container as described in the <a href="https://docs.docker.com/docker-for-mac/" rel="noreferrer">docker for mac quickstart</a>, and I can access ports from the container via localhost just fine.</p>