Having problems accessing port 5000 in Vagrant

verbsintransit picture verbsintransit · Apr 22, 2014 · Viewed 12.6k times · Source

I am trying to teach myself Flask in a Vagrant environment. I understand that Flask runs a server on port 5000 by default. In my Vagrantfile I have:

config.vm.network :forwarded_port, guest: 80, host: 8080
config.vm.network :forwarded_port, guest: 5000, host: 5000

I have a simple tutorial Flask app:

from flask import Flask 
app = Flask(__name__)

@app.route('/hello')
def hello_world():
    return 'Hello world!'

if __name__ == '__main__':
    app.run(debug=True)

Yet when I run python hello.py in my Vagrant environment and subsequently go to 127.0.0.1:5000/hello in Chrome on my desktop, I can't connect.

I don't know nearly enough about networking. What am I missing?

Answer

Joben R. Ilagan picture Joben R. Ilagan · Sep 27, 2014

If you are accessing from Chrome in your desktop, you are technically accessing from a different computer (hence you need to place host='0.0.0.0' as argument to app.run() to tell the guest OS to accept connections from all public (external) IPs.

This is what worked for me (for both 127.0.0.1:5000/hello and localhost:5000/hello in Chrome):

from flask import Flask
app = Flask(__name__)

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

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