0

I have a toy Flask app:

from flask import Flask

app = Flask(__name__)

@app.route("/home")
def home():
    return "<h1>Home...</h1>"

@app.route("/health")
def health():
    return "<h1>Healthy</h1>"

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=5000, debug=True)

.. which is being provisioned using Ansible on a Vagrant guest machine with the following Vagrantfile:

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure(2) do |config|
  config.vm.box = "geerlingguy/centos7"
  config.vm.provision "ansible_local" do |ansible|
    ansible.playbook = "playbooks/run.yml"
    ansible.tags = "install"
  end
  config.vm.network "forwarded_port", guest: 5000, host: 5000
  config.vm.provider "virtualbox" do |v|
     v.memory = 2048
     v.cpus = 1
  end
end

If I vagrant ssh to the guest and launch the flaskapp in with the following:

export FLASK_APP='/flaskapp/app.py'
export FLASK_ENV='development'
cd  /flaskapp
python3 -m flask run

I can curl 127.0.0.1:5000/home with a successful response.

However from the Vagrant host (i.e. not the guest where the flask app is running), I cannot access http://localhost:5000/home. Accessing localhost:5000/home (or 127.0.0.1:500/home) is:

The connection was reset

The connection to the server was reset while the page was loading.

    The site could be temporarily unavailable or too busy. Try again in a few moments.
    If you are unable to load any pages, check your computer’s network connection.
    If your computer or network is protected by a firewall or proxy, make sure that Firefox is permitted to access the Web.

I'm using Windows 10, and the guest VMs network config looks like this:

Guest network config

I have tried assigning static ip or assigning an ip dynamically but I cannot access. I am not sure if it's something that I need to add to the /etc/hosts file...

Can anyone help to identify the issue here and how can I debug this kind of problem?

user3535074
  • 1,268
  • 8
  • 26
  • 48
  • did you forward the 5000 port on your windows machine ? Else the url to connect to your flask app will be `http://:5000/home` where ip must be on a bridge or hostonly adapter. See https://www.virtualbox.org/manual/ch06.html#networkingmodes. You want to have a look at [Vagrant Host Manager](https://github.com/devopsgroup-io/vagrant-hostmanager) that can help mitigate the administration burden and help use easier local fqdns (although its support for windows seems partial...) – Zeitounator Jun 19 '22 at 00:18
  • Is that not what `config.vm.network "forwarded_port", guest: 5000, host: 5000` does? I will try a host only and bridged network... – user3535074 Jun 19 '22 at 06:47
  • Yes it should and I overlooked it sorry. But you should check it has been done correctly in the gui (button "Port Forwarding" in your NAT network configuration). You should also describe with much more precission what `I cannot access http://localhost:5000/home` means. What is the exact error you get? (add it in an edit). – Zeitounator Jun 19 '22 at 08:29
  • The virtualbox VM shows that the port forwarding is in effect. I also updated the question. – user3535074 Jun 19 '22 at 20:34
  • I think the problem is that though the `app.py` file specifies to run the app on the `0.0.0.0` host, the command `python3 -m flask run` is not executing that code, or at least the `0.0.0.0` host is not respected. If I use the command `python3 -m flask run --host 0.0.0.0`, the http requests from outside the VM arrive to the web server and the results are displayed in the browser. – user3535074 Jun 21 '22 at 18:59
  • What about `python3 yourapp.py`? – Zeitounator Jun 21 '22 at 21:50
  • Would be interested if you ever get a resolution - v similar to my issue here https://stackoverflow.com/questions/60945984/cant-access-localhost-with-vagrant-port-forwarding – Simeon Sep 04 '22 at 12:58

0 Answers0