31

I use Vagrant when developing websites and have done for the last year or so. I would like to test out some functionality on an iPad but I can't figure out how to get the iPad to connect to the Vagrant box on my Mac.

Is it possible to do this?

Craig Ward
  • 2,425
  • 5
  • 33
  • 51

3 Answers3

24

If you configure your network this way in the Vagrant file:

config.vm.network :forwarded_port, guest: 4000, host: 4000

And then serve files on that port. You will be able to hit it externally. Assuming you are running the vagrant box on a Mac it would be (machine).local:4000, for example I'm joe.local:4000

Quite convenient to be able to use an iPad and an iPhone as your preview monitors as you work on a responsive site, if you have livereload or equivalent going you don't even need to touch them.

renegade
  • 423
  • 3
  • 7
  • 1
    how about accessing different virtual hosts on your vm? – user487772 Dec 09 '13 at 11:13
  • @Tim do you really mean different virtual machines, or different ports on the same machine? For multiple VMs you would just use different ports in each VM config. For multiple services on the same VM you configure multiple forwarded ports. Also it is not necessary for the port numbers on the inside and outside to match. – renegade Dec 09 '13 at 17:46
  • 3
    No, I mean the case when I have multiple apache virtual hosts inside of my VM (e.g. test1.loc, test2.loc) – user487772 Dec 09 '13 at 20:31
  • 1
    omg unbelievable it worked! Thanks! I forwarded port 22 to port 2223 and was able to ssh into my desktop VM from my laptop. Excellent! – B T Sep 09 '14 at 03:29
10

Take a look at Bridged Networking in the Vagrant documentation. Enabling it will allow your VM to appear as a device on the network, and allow you to connect to its IP address and the port your web server is listening on.

Huge caveat: you haven't given any information about your local network, so it's entirely likely that the IP of the VM will not be reachable to your iOS device, which is likely connected via WiFi--and many routers' factory configurations do not allow communication between devices connected wirelessly and those connected via ethernet.

But it's possible.

cgeisel
  • 255
  • 2
  • 9
  • Yeah I have already tried this. While this is the correct answer I have a problem that the site are all geared up to use domain names and therefore I still get issues with things not loading. However, thats another problem, probably solved by a proxy. Thanks for the answer :) – Craig Ward Aug 31 '12 at 08:47
  • Is the issue that your iPad will only connect to a hostname (ie somedomain.com, rather than an IP address)? If so, you may be able to connect to your Mac using its .local hostname. Usually it's in the format "username.local", where username is something like "craig" or "cward" depending on how you set up your account. – cgeisel Feb 27 '13 at 03:28
  • 1
    The URL refenced above is no longer valid. Maybe one of these two would be useful. https://www.vagrantup.com/docs/networking/public_network.html https://friendsofvagrant.github.io/v1/docs/bridged_networking.html – Craig London Apr 13 '17 at 15:20
8

This is an old question but for anyone using vagrant 1.5 or greater you can just use vagrant share

AntonioCS
  • 8,335
  • 18
  • 63
  • 92