The flask host adress in docker run

2024/10/9 12:34:46

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?

Answer

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
...
https://en.xdnf.cn/q/70019.html

Related Q&A

Extracting text from pdf using Python and Pypdf2

I want to extract text from pdf file using Python and PYPDF package. This is my pdf fie and this is my code:import PyPDF2 opened_pdf = PyPDF2.PdfFileReader(test.pdf, rb)p=opened_pdf.getPage(0)p_text= p…

Is it possible to change turtles pen stroke?

I need to draw a bar graph using Pythons turtle graphics and I figured it would be easier to simply make the pen a thick square so I could draw the bars like that and not have to worry about making doz…

How to make a local Pypi mirror without internet access and with search available?

Im trying to make a complete local Pypi repository mirror with pip search feature on a server I can only connect an external hard drive to. To be clear, I dont want a simple caching system, the server …

Turn an application or script into a shell command

When I want to run my python applications from commandline (under ubuntu) I have to be in the directory where is the source code app.py and run the application with commandpython app.pyHow can I make i…

pytest - monkeypatch keyword argument default

Id like to test the default behavior of a function. I have the following:# app/foo.py DEFAULT_VALUE = hellodef bar(text=DEFAULT_VALUE):print(text)# test/test_app.py import appdef test_app(monkeypatch):…

How remove a program installed with distutils?

I have installed a python application with this setup.py:#!/usr/bin/env pythonfrom distutils.core import setup from libyouandme import APP_NAME, APP_DESCRIPTION, APP_VERSION, APP_AUTHORS, APP_HOMEPAGE,…

How to check which line of a Python script is being executed?

Ive got a Python script which is running on a Linux server for hours, crunching some numbers for me. Id like to check its progress, so Id like to see what line is being executed right now. If that was …

input to C++ executable python subprocess

I have a C++ executable which has the following lines of code in it /* Do some calculations */ . . for (int i=0; i<someNumber; i++){int inputData;std::cin >> inputData;std::cout<<"T…

pandas extrapolation of polynomial

Interpolating is easy in pandas using df.interpolate() is there a method in pandas that with the same elegance do something like extrapolate. I know my extrapolation is fitted to a second degree polyno…

Speed-up a single task using multi-processing or threading

Is it possible to speed up a single task using multi-processing/threading? My gut feeling is that the answer is no. Here is an example of what I mean by a "single task":for i in range(max):p…