Home > Blockchain >  Flask doesn't render html page from docker [duplicate]
Flask doesn't render html page from docker [duplicate]

Time:10-01

I have a simple flask app that works without docker but doesn't reply when being wrapped in docker container. I suspect I have some addresses messed up

from flask import Flask, render_template, request
import pandas as pd

app = Flask(__name__)

@app.route('/', methods=['GET', 'POST'])
def uploadfile():
    if request.method == 'POST':  # check if the method is post
        df = pd.read_json(request.files.get('file'))
        return render_template('upload.html',  tables=[df.head().to_html(classes='data', header="true")])
    return render_template('upload.html')


if __name__ == '__main__':
    app.run(debug=True, port=5000)  # running the flask app

upload.html

<!doctype html>
<html>
  <head>
    <title>File Upload</title>
  </head>
  <body>
    <h1>File Upload</h1>
    <form 
      method="POST" 
      action="/" 
      enctype="multipart/form-data"
    >
      <p><input type="file" name="file"></p>
      <p><input type="submit" value="Submit"></p>
    </form>
    {% for table in tables %}
            {{ table|safe }}
    {% endfor %}
  </body>
</html>

The project tree:

├── script.py
├── templates
    └── upload.html

Dockerfile:

FROM python:3.7-slim-buster

WORKDIR /app

COPY requirements.txt ./


RUN apt-get update 
RUN pip install --no-cache-dir -r requirements.txt


RUN mkdir /templates

COPY script.py /app/
COPY ./templates/ /app/templates/

RUN dir

CMD [ "python", "script.py" ]

and I launch the docker image as

docker run -it --rm -p 5000:5000 online-app

The the logs are as follows:

* Serving Flask app "script" (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: on
 * Running on http://127.0.0.1:5000/ (Press CTRL C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 416-968-407

I go to the http://127.0.0.1:5000/ and the page is loading forever. What did I miss?

UPD: Using app.run(debug=True, host=0.0.0.0, port=5000) # running the flask app has logs like:

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

But the link is dead as well

CodePudding user response:

change listening host IP to 0.0.0.0

app.run(debug=True, host='0.0.0.0', port=5000)  # running the flask app

When you trying to connect to http://127.0.0.1:5000 - you trying to connect to localhost interface of your physical server (docker host) But flask application listening only inside-container's localhost, what is not accessible outside container

CodePudding user response:

I'm no expert with Flask especially, but according to your logs, it does not seem to listen on the correct address. Indeed, inside a container, 127.0.0.1 will not match with requests coming from outside the container (it would work with a Curl from inside the container for instance). In order to make it listen to "non-local" (as in "not originating from the container itself") requests, it has to listen on 0.0.0.0

  • Related