简体   繁体   中英

Flask app runs on docker but not from the host

I have a flask app that runs a couple of things based on the user call. In this app, I also have one basic endpoint that just returns a string which would help me to see if the app is running or not.

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

Now I tried to dockerize this app using this tutorial from the official docker site. The flask app listens on the port 5000 and so I added an entry in my Dockerfile to expose the port 5000.

EXPOSE 5000

And the command that I am using in the Dockerfile to run the app is

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

model.py file has the flask code that calls other functions based on the user input.

Now when I run my app after containerizing it, I see the required output on the terminal that the flask app is indeed running. This is the command that I used to run the app.

docker run -p 5000:5000 firstContainer

When I try to call the basic helloWorld method above by using the request http://localhost:5000/hello , I get an error message saying that the site is unavailable. Is there anything that I am doing wrong wrt the port mappings here? How do I fix this issue ?

EDIT: Adding more details

So I tried to go into the container to see what's happening and I was able to view the files that were available on the container and they look good. When I tried to start the app again in the container using the base command

python model.py

it returned an error saying that the port is already in use. So this should mean that the app is indeed listening on the port. I also installed curl inside the container to browse the URL and it returned the expected string when I ran it inside the container. I just don't understand how I can expose it to outside world

EDIT 2:

Container logs

 * Serving Flask app "model" (lazy loading)
 * Environment: production
   WARNING: Do not use the development server in a production environment.
  Use a production WSGI server instead.
 * Debug mode: on

The problem is the server defaults to answer internal requests only. You have two options.

You can set the host programatically to:

0.0.0.0

Use the CMD:

flask run --host=0.0.0.0`

Tip: There is also a container running flask behind ngnix out-of-the-box.

try this :

docker run --rm -it --network=host -p 5000:5000 firstContainer

the problem is probably related to networking. so --network=host connects the container to underlying host.

I have the same issue on my Flask App in the docker container. I have used --network=host and resolved the issue.

# docker run -it -p 8080:8080 --network=host mbilgen/metacriticv3:1.0
WARNING: Published ports are discarded when using host network mode
 * Serving Flask app "main" (lazy loading)
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Running on http://127.0.0.1:8080/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 258-420-336
127.0.0.1 - - [12/May/2019 03:55:08] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [12/May/2019 03:55:13] "GET /games HTTP/1.1" 200 -
INFO:werkzeug:127.0.0.1 - - [12/May/2019 03:55:13] "GET /games HTTP/1.1" 200 -

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM