2

I can't access my forwarded ports outside my vagrant box shell.

I created a vagrant box with the following forwarded ports.

  config.vm.network :forwarded_port, guest: 80, host: 8080, auto_correct: true
  config.vm.network "forwarded_port", guest: 27017, host: 27017 #mongodb
  config.vm.network "forwarded_port", guest: 5000, host: 5000 #python flask port
  config.vm.network "forwarded_port", guest: 5050, host: 5050 # NODE Port

  config.vm.network "private_network", ip: "192.168.33.10"

I have installed both a python server and a nodejs server. If I ssh in to my vagrant box and try: curl http://localhost:5050 it correctly returns my nodejs hello world. If I curl http://localhost:5000 it correctly my python3 hello world.

However, I can't access my localhost outside the vagrant environment (with the servers in vagrant still active)

What am I doing wrong?

I am setting up a new machine - so it is possible I've not installed something essential on my localmachine but I have installed the dependencies for vagrant etc.

Pinging localhost or 127.0.0.1 works but only without a port. I am getting the following errors in terminal from my host machine outside vagrant:

$ ping localhost:80
ping: cannot resolve localhost:80: Unknown host
$ ping localhost:8080
ping: cannot resolve localhost:8080: Unknown host

$ curl 'http://localhost:80'     
curl: (7) Failed to connect to localhost port 80: Connection refused
$ curl 'http://localhost:8080' 
curl: (56) Recv failure: Connection reset by peer
$ curl 'http://localhost:5000'
curl: (56) Recv failure: Connection reset by peer
$ curl 'http://localhost:5050'
curl: (56) Recv failure: Connection reset by peer
$ curl 'http://127.0.0.1:80'    
curl: (7) Failed to connect to 127.0.0.1 port 80: Connection refused
$ curl 'http://127.0.0.1:8080'
curl: (56) Recv failure: Connection reset by peer
$ curl 'http://127.0.0.1:5000'
curl: (56) Recv failure: Connection reset by peer
$ curl 'http://127.0.0.1:5050'
curl: (56) Recv failure: Connection reset by peer

& the below from the static ip, after a short delay for each...

$ curl 'http://192.168.33.10:80'
curl: (7) Failed to connect to 192.168.33.10 port 80: Connection refused
$ curl 'http://192.168.33.10:8080'
curl: (7) Failed to connect to 192.168.33.10 port 8080: Connection refused
$ curl 'http://192.168.33.10:5000'
curl: (7) Failed to connect to 192.168.33.10 port 5000: Connection refused
$ curl 'http://192.168.33.10:5050'
curl: (7) Failed to connect to 192.168.33.10 port 5050: Connection refused

I am using Mac OS Catalina. My firewall is disabled & allowing all connections.

Output of sudo netstat -ntlp within the vm..

tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      469/rpcbind         
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN      510/systemd-resolve 
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      865/sshd            
tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN      790/postgres        
tcp        0      0 192.168.33.10:5050      0.0.0.0:*               LISTEN      2730/node           
tcp        0      0 192.168.33.10:5000      0.0.0.0:*               LISTEN      4070/python         
tcp6       0      0 :::111                  :::*                    LISTEN      469/rpcbind         
tcp6       0      0 :::22                   :::*                    LISTEN      865/sshd            
tcp6       0      0 ::1:5432                :::*                    LISTEN      790/postgres

It seems to be similar to the problem here but my vagrantfile has the right lines in it: Vagrant, nodejs and iptable port forwarding

I've also double checked everything pointed out in about 10 questions like this: vagrant port forwarding doesn't work: Connection reset by peer

Simeon
  • 848
  • 1
  • 11
  • 34
  • Just FYI, `ping` uses ICMP, which doesn't have ports like TCP. You should be able to `ping localhost` or `ping 192.168.33.10`, but `ping localhost:8080` will never work. – n7s Apr 03 '20 at 17:25
  • Also, because you've assigned the VM an IP address on a private network you shouldn't need to setup port forwarding. Instead (once your issue is resolved), you can just `curl 192.168.33.10:5000` and `curl 192.168.33.10:5050` directly. – n7s Apr 03 '20 at 17:27
  • 1
    Assuming your Vagrant VM is a Linux machine, can you show us the output of `sudo netstat -ntlp`? It would be beneficial to see what services are listening on what ports, e.g. perhaps your Python and NodeJS services are only listening on 127.0.0.1 - which means they wouldn't respond to requests to 192.168.33.10. Just a thought. – n7s Apr 03 '20 at 17:29
  • Thanks for the pointers - I've added the output of netstat. Also, useful to know that pinging localhost with a port won't work. The only reason I tried a private IP was because I couldn't make sense of localhost. I'd like to be able to curl localhost:5000 (etc) or visit it in my browser! – Simeon Apr 04 '20 at 08:24
  • have you tried specifying the host in both your flask and node app to '0.0.0.0' ? – SLIMANI Mohammed Sep 17 '20 at 07:57
  • Was this ever solved? – Dreamystify Jul 12 '22 at 02:17
  • @Dreamystify Sadly not. I've had issues with port forwarding with vagrant for years so I think, as much as I like what it does (when it works), I am past using it now. – Simeon Sep 04 '22 at 12:48

0 Answers0