Having problems accessing port 5000 in Vagrant Having problems accessing port 5000 in Vagrant flask flask

Having problems accessing port 5000 in Vagrant


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 Flaskapp = Flask(__name__)@app.route("/hello")def hello():    return "Hello World!"if __name__ == "__main__":    app.run(host='0.0.0.0')


You possibly need to get Flask to serve on an externally-visible URL: see the docs


This may be caused by Vagrant (VirtualBox) NAT Port forwarding not working properly (port conflicts).

To narrow down the issue, you may want to make sure that port 5000 is open correctly on both end, this can be done by using nmap, nc (netcat) or netstat etc.

e.g. on host

nmap 127.0.0.1nc -vz 127.0.0.1 5000curl http://127.0.0.1:5000

Within the guest

nmap GUEST_IPnc -vz GUEST_IP 5000curl http://GUEST_IP:5000

NOTE: GUEST_IP is most likely in the 10.0.2.0/24 network (vbox NAT engine default).

Running these commands on both your host and within the VM (guest box) will tell you if the ports are open.

Make sure your python hello world binds NOT ONLY to the loopback device so that it can serve requests from external clients.

Use lsof -i :5000 or netstat -nap | grep :5000 to determine which program is binding the port for further troubleshooting.