Reputation: 1605
I'm running an app on my local box via Vagrant. The Python/Flask app launches and prints:
* Running on http://127.0.0.1:5000/
* Restarting with reloader
I found this https://github.com/makersquare/student-dev-box/wiki/Using-Vagrant-for-Development#testing-web-based-applications-in-vagrant
which suggests that Vagrant apps run on 10.10.10.10
(not 127.0.0.1
), but when I navigate to that IP address (port 5000), I get the same result: "This webpage is not available".
Question: my app is running, but on what IP address? I can't seem to find it. Do I need to modify some configuration files?
Thanks in advance.
Upvotes: 5
Views: 3843
Reputation: 41
In the latest version of Flask and python you do not need to configure vagrant file or no change required in the python script. Just run the flask with --host
flask run --host=192.168.10.80
Upvotes: 1
Reputation: 11
sjudǝʊ is right but it took me 4 hours to figure out he forgot to mention you also must run:
vagrant halt
and then vagrant up
in order for your update to the vagrant file to actually take affect
Upvotes: 1
Reputation: 1565
There are many ways how you could run flask web app on virtual machine (managed by vagrant). I think that following approach is quite flexible, because you don't have to deal with different ip address. Also it looks like you are developing on a host machine.
There are 2 things you need to configure. In VagranFile, you need configure port forwarding.
Vagrant.configure(2) do |config|
# use default box
config.vm.box = "ubuntu/trusty64"
# forward port guest machine:5000 -> host machine:5000
# port 5000 is default for flask web app
config.vm.network "forwarded_port", guest: 5000, host: 5000
end
Then, on virtual machine, you should start flask app on ip 0.0.0.0
which means that web app will serve for any IP address. More on this topic -> flask doc section Externally Visible Server
if __name__ == "__main__":
app.run("0.0.0.0", debug=True)
That's it. You should be able to connect to http://localhost:5000
Upvotes: 9
Reputation: 5682
In the file where you call app.run
, it should be
app.run(host='0.0.0.0', port=...
In the host OS, navigate to the IP of the guest with the port that you're running the app from.
Upvotes: 1