Docker Flask, stuck at "Waiting response from localhost"

Viewed 1032

Im trying to run a test Flask in Docker, but for some reason, i cant connect to it from the host machine, the composition of files is:

pyweb/
 |app.py
 |Dockerfile
 |___app/
     |__init__.py

The files content: Dockerfile:

FROM python:2.7
COPY . /pyweb
WORKDIR /pyweb
RUN pip install flask
RUN export FLASK_APP=app.py
ENTRYPOINT ["python"]
CMD ["app.py"]

app.py:

from flask import Flask
from app import app 

app/init.py:

from flask import Flask

app = Flask(__name__)


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

app.run(host='0.0.0.0')

After running the docker file, and starting the container with docker run -d -p 5000:5000, the response is

* Serving Flask app "app" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)

But i cant connect form the host machine, it doesnt respond

3 Answers

Ok so I had that same issue during an interview a bit more than a year ago and it still puzzles me to this day.

I don't know why it's not working as expected when running the flask app with app.run().

Somehow it works fine when starting the app with the flask command line directly.

The Dockerfile would look like this:

FROM python:2.7
COPY . /pyweb
WORKDIR /pyweb
RUN pip install flask
ENV FLASK_APP=app.py
CMD ["flask", "run", "--host", "0.0.0.0"]

And you can drop app.run(host='0.0.0.0') from the __init__.py file.

I'll probably spend some time later trying to understand why your original implementation doesn't work as expected. I don't know much about flask but I don't see anything wrong in your code.

You should mention the port in flask app and set debug = True

app.run(host='0.0.0.0', port=5000, debug = True)

You should connect to the same port

docker run -p 3000:5000

Now you can access your app at http://localhost:3000/

This might not answer the OP's question but might answer others' questions who came here..

For me, the Flask output showed that the application was running after launching the Docker container. It showed this line, for example:

Running on http://172.17.0.2:5000/ (Press CTRL+C to quit)

So, I clicked this link and expected it to work.

However, you need to connect to the Docker container --- not the Flask app. Going to http://172.17.0.2:5000/ connects to the Flask app directly, which won't work. You need to go to http://0.0.0.0:5000/, which connects to the Docker container, which connects to the Flask app.

So going to http://0.0.0.0:5000/ worked.

Related