7

I want to run a flask application in Docker, with the flask simple http server. (Not gunicorn)

I got a host setting problem.

In the flask app.py, it should be work as the official tutorial, but it doesn't work:

if __name__ == '__main__':
    app.run(host='0.0.0.0')

So I did it after an answer of a similar post, it suddenly works!:

https://stackoverflow.com/a/43015007/3279996

$> flask run --host=0.0.0.0

My question is why this first method doesn't work, but second works?

xirururu
  • 5,028
  • 9
  • 35
  • 64

1 Answers1

1

In the first solution, when you run the code with the python3 app.py command, it runs on 0.0.0.0 on the host. But the flask run command executes the flask app directly from the code and does not include the condition if name == 'main':. On the other hand, when you run the code with python3 app.py, name equals 'main'. But when you run the code with flask run, name is equal to the FLASK_APP variable that becomes an app in your code, and the FLASK_APP variable must be the same as the file name that flask app contains.

app.py

print("__name__ is :", __name__)

python3 app.py

__name__ is : __main__

app.py

$> export FLASK_APP=app.py
$> flask run

...
__name__ is : app
...
Cosmos
  • 372
  • 2
  • 6