Vagrant: virtualbox provider ignores host_ip for forwarded ports

Created on 9 Oct 2014  ·  16Comments  ·  Source: hashicorp/vagrant

I have two sample Vagrantfiles in different folders, for different project.
One specifies

config.vm.network "forwarded_port", guest: 3000, host: 3000, host_ip: "127.1.1.253" 

and other

config.vm.network :forwarded_port, guest: 3000, host: 3000, host_ip: "127.1.1.254"

When I run only one, everything is all right. But when I try to get up second, vagrant tells me that port is busy:

Bringing machine 'default' up with 'virtualbox' provider...
==> default: Checking if box 'ubuntu/trusty64' is up to date...
==> default: Clearing any previously set forwarded ports...
Vagrant cannot forward the specified ports on this VM, since they
would collide with some other application that is already listening
on these ports. The forwarded port to 3000 is already in use
on the host machine.

To fix this, modify your current projects Vagrantfile to use another
port. Example, where '1234' would be replaced by a unique host port:

  config.vm.network :forwarded_port, guest: 3000, host: 1234

Sometimes, Vagrant will attempt to auto-correct this for you. In this
case, Vagrant was unable to. This is usually because the guest machine
is in a state which doesn't allow modifying port forwarding.

But netstat -nl shows only one record for tcp:3000 and it's on correct address:

$ netstat -nl | grep 3000
tcp        0      0 127.1.1.254:3000        0.0.0.0:*               LISTEN  

What am I doing wrong?

UPD: Vagrant 1.6.5

bug has-pr providevirtualbox

Most helpful comment

I've fixed this problem in #7035.

All 16 comments

it's written in the error message
Vagrant cannot forward the specified ports on this VM, since they
would collide with some other application that is already listening
on these ports

and you can see it in your netstat list. you simply cannot have the port 3000 opened 2x on host machine. you can have it on guest machines, though. change your second config to for example:

config.vm.network :forwarded_port, guest: 3000, host: 3001, host_ip: "127.1.1.254"

@ulkas, no. You see, I have (and you too ;)) a whole 127./8 block of addresses for loopback connections, so, I'm using an address per app; it's very convinient, if you also make the record in /etc/hosts.

So, actualy, I can:

$ netcat -l -p 3000 -s 127.1.1.1 &> /dev/null &
[1] 27418
$ netcat -l -p 3000 -s 127.1.1.2 &> /dev/null &                                                                       
[2] 27458
$ netstat -nl | grep 3000
tcp        0      0 127.1.1.2:3000          0.0.0.0:*               LISTEN     
tcp        0      0 127.1.1.1:3000          0.0.0.0:*               LISTEN   

Question is, why Vagrant doesn't allow that? =)

Fixed by #4616

Had to revert that PR. I'll take another look at this.

I'm not seeing where host_ip is even a thing? In the documentation I see it referenced as simply "ip:" and this applies to private and public networks NOT to forwarded ports. Logically if you are forwarding ports from the host, it can only have ONE of any port in use, even if the guests have multiple IPs.

@dragon788 hopefully this link works, but in the Options Reference it lists both guest_ip and host_ip.

(edit: added later: … aaaand after looking at #5587 I see you saw the info already. I'll slink away, now…)

. Logically if you are forwarding ports from the host, it can only have ONE of any port in use

@dragon788 looks like you misunderstand the concept of a port.

Look at Wikipedia page.

In computer networking, a port is a software construct serving as a communications endpoint in a computer's host operating system. A port is _always associated with an IP address of a host_ and the protocol type of the communication.

It's an abstraction - you cannot bind to just a port. When you "bind to a machine port" (like Vagrant does right now - hence this issue) you bind to special IP address 0.0.0.0 which means "any address".

So, I want to have ability to bind app to a socket with another host machine IP address. And you almost always have plenty of them even if you are not connected to any network, because alot of operating systems (Linux, Mac, Windows) has special "loopback" network device and whole 127/8 network associated with it, which has 16,777,216 addresses. Multiply it by 65535 (number of ports you can bind to per address) and you have 1099494850560 possible combinations of an ip:port IPv4 TCP (and UDP) sockets within only 127/8 network.

Also consider that, sometimes, you don't want your forwarded ports to be accessable to a people from external (or even yours) network - for example, you're using the Vagrant as some kind of a developer. You can use firewall for this, ofc, but why don't you just bind the forwarding to a local-only accessable ip address and ensure that your port forwardings are still (mostly) secure even if you misconfigure the firewall.

Having this issue after attempting to fix conflicts between VMs. Went for the host_ip fix, found it doesn't work, found this bug.

confirmed, host_ip doesn't work on VBox. Tried to bind the forwarding port only to loopback (127.0.0.1), and instead get no port forward at all. If I remove the host_ip: option, the forwarding works correctly.

Yea, it doesn't work, I'm trying to have the following setup (except that I'm using a different guest port numbers but same host port numbers)

My host machine's ethernet interface is IP aliased to have multiple IP addresses configured (172.22.8.0/24). I'm spinning up a Virtual machine with the following port forwarding rule.

srv.vm.network "forwarded_port", host_ip: 172.22.8.200,  host: 32977, guest: 1777
srv.vm.network "forwarded_port", host_ip: 172.22.8.201,  host: 32977, guest: 2777

Vagrant only actions the last rule, doesn't matter if I have 10 or 50 of those port forwarding rules.

Wow, this issue is still open.

What's really seems strange to me that here and in #7017 people seems to say the terrible misconception - "you cannot bind to the same port twice". There is no "binding to the port" concept, you simply cannot bind to the port without the protocol and address.

I find saying "nonono, there is no true addresses except 0.0.0.0 that you can bind to" to be really limiting the developer in the tool which main stated purpose is "create and manage complete portable development environments".

This is not an issue for me anymore beacuse I've switched to Docker some time ago - it is sufficient for my use case (I use Linux as host machine so with it I also don't get the VM overhead for my dev setups, provided I don't need the "real vm" isolation level) and it does allow you to specify host ip to bind to when you're forwarding ports.

Not saying that everyone have to switch there, just saying what worked for me.

I still hope the issue will get fixed some day and I really wish good luck to guys that still have this problem. :)

I've fixed this problem in #7035.

Having this issue after attempting to use Django on a vagrant VM. Went for the host_ip fix, found it doesn't work, found this bug. Looking forward to the PR getting accepted.

Fixed via #7035

This appears to be still be an issue with Vagrant 1.9.5.

I tried downgraded to 1.9.2 and still have this issue. Anyone else?

I'm going to lock this issue because it has been closed for _30 days_ ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

Was this page helpful?
0 / 5 - 0 ratings