Using Vagrant, access app on host machine from inside Vagrant - vagrant

I am running a Wordpress app inside a Vagrant box that needs to access a Rails app running on my host machine (using Pow). How do I do that?
I can access the Rails app on my host machine using myapp.dev and would like to access it from inside Vagrant as well, in the best case using the same domain.
All info I could find on that topic is about the other way around, where you want to access an app running inside Vagrant from the host machine.

So it just sounds like you need a host entry on your guest machine pointing back your host machine?
You could do something like the following:
Vagrant.configure("2") do |config|
require 'socket'
local_ip = UDPSocket.open {|s| s.connect("64.233.187.99", 1); s.addr.last}
config.vm.provision "shell", inline: "echo \"#{local_ip} myapp.dev\" >> /etc/hosts"
end
This should use a socket to get your host machines IP address and then using a shell provisioner it will append that IP address to your hosts file which should allow your VM to use the domain name you want to communicate with your host OS.

Related

Ubuntu - vagrant access from another computer

I have recently learned how to setup a virtual machine using vagrant (virtualbox), and I know how to access it from a web browser via the local adress such as 192.168.x.x:9292 since 9292 is the default gate. But I would like to access the server from another computer locally.
My question is if I could access it via cmd as I connect to it on my own with the command vagrant ssh but from another computer via a command like vagrant ssh 192.168.x.x?
As far as I understand you have to use external programs or setup something inside the vagrant environment? Is there a simple way to access the server or is it password protected? If I setup a website I can access that for example from another local machine but can I access files on the server (from another computer)?
I found how to connect to another computer locally by entering the vagrant environment, vagrant up --> vagrant ssh. And connected to another computer by typing in ssh vagrant#192.168.x.x where 192.168.x.x is the local address to the computer. The vagrant asks for a password and you type in the default password which I believe is vagrant and then you are connected to the other vagrant instance.
Assuming you have virtualbox provider for Vagrant you can achieve this by doing bridging the VBox network interface with Host.
Use this in Vagratfile (for public/private)
For Public
Vagrant.configure("2") do |config|
config.vm.network "public_network",
use_dhcp_assigned_default_route: true
end
For Private
Vagrant.configure("2") do |config|
config.vm.network "private_network", type: "dhcp"
end
above code will create IP in the range of Host network and using this you can access the file server from another machine.

Publicly accessible IP for Vagrant on OSX

Trying to learn a bit more about networking via Vagrant and Linux.
My machine is running OSX and I've spun up a Vagrant box (trusty 64) and set up a simple netcat redirect like so:
printf 'HTTP/1.1 302 Moved\r\nLocation: https://www.eff.org/' | nc -l 2345
I'm trying to get netcat to redirect my browser when I hit it (IP:2345) but nothing seems to work. I've tried looking up the IP from Vagrant a bunch of times and I keep finding different addresses and none of them work. Browser simply hangs and netcat never receives the request.
I've very little experience in VMs so I'm guessing that there isn't a publicly accessible IP in this instance? At least not with some config.
Solution is to bridge the network. Add this to the Vagrantfile:
Vagrant.configure("2") do |config|
config.vm.network "public_network"
end
Documentation on this here:
https://www.vagrantup.com/docs/networking/public_network.html

How to capture the IP address of the guest machine in Vagrant

I am working on a project where I am creating rackspace cloud instances using Vagrant. I am using vagrant-rackspace plugin to do it. Everything works fine for me except the part that I need to capture the IP of the new rackspace instance created in the vagrantfile.
I need this IP address in order to add it to the Ansible inventory file (/etc/ansible/hosts)
Here is my Vagrantfile:
Vagrant.configure(2) do |config|
config.ssh.private_key_path = "rackspace_rsa"
config.vm.provider :rackspace do |rs|
rs.username = "rackspace-user"
rs.api_key = "a98weqrq3r34ewfadsf43rffa4697cc0036"
rs.admin_password = "Password1"
rs.flavor = /1 GB Performance/
rs.image = /Ubuntu 12.04/
rs.rackspace_region = :dfw
rs.server_name = "ansible_server_1"
rs.public_key_path = "rackspace_rsa.pub"
end
end
Once the instance is created I am going to provision the machine using Shell provisioning to install ansible. After installing ansible I will update the inventory file to reflect the IP of the newly created rackspace instance.
You could use the Vagrant Host Manager plugin using the Custom IP Resolver
Custom IP resolver gives you oportunity to calculate IP address for each machine by yourself, giving You also access to the machine that is updating /etc/hosts. For example:
config.hostmanager.ip_resolver = proc do |vm, resolving_vm|
if hostname = (vm.ssh_info && vm.ssh_info[:host])
`host #{hostname}`.split("\n").last[/(\d+\.\d+\.\d+\.\d+)/, 1]
end
end
#FrédéricHenri I am adding this as an answer since it is too long for a comment.
Well, my plan was to spin up three virtual machines on rackspace (VM-ONE,VM-TWO and VM-THREE).
-> I installed vagrant on my macbook
-> Wrote a vagrantfile to spinup the first box(VM-ONE)
-> install vagrant and ansible on VM-ONE using vagrant shell provisioning
-> Write vagrantfile to spin up VM-TWO and VM-THREE
-> Write playbook to provision VM-TWO and VM-THREE
-> Run "vagrant up" on VM-ONE
-> Use vagrant ansible to provision VM-TWO and VM-THREE.
I hit a roadblock here.
I needed to create an inventory file which contains IPs of VM-TWO and VM-THREE.
I figured that Vagrant provides a default inventory file that contains the IPs of the two instances it creates and ansible will use that inventory file while running the playbook.
The inventory file auto generated by vagrant sits at .vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory.
So my original problem is resolved now. Thank you.

How to access Redis running in Vagrant Virtual Machine

We are trying to use Vagrant to run a Redis server on Mac (using an Ubuntu Guest OS) with this Vagrantfile
Sadly we are unable to access the Redis database inside the Vagrant Box.
We get this error:
Error: Redis connection to 127.0.0.1:6379 failed - read ECONNRESET
This is the Network configuration in the VirtualBox VM:
What else do we need to add to the Vagrantfile to expose Redis to Mac?
(Note: the reason for using Vagrant is both to let people try redis without having to install it on their main OS, but more importantly to ensure that other elements of the app run as expected)
This may be helpful https://serverfault.com/questions/248248/cannot-connect-to-redis-installed-on-virtualbox-running-ubuntu-from-windows-7. I know question is about connecting from Windows, but the solution is mostly modifications to the Redis config within the VM in order to not bind Redis only to a local port in the redis.conf so that it can be accessed on the host machine(Mac in your case).
Also, depending on how you are trying to access Redis you may be able to configure a SSH tunnel on the host machine(Mac) in order to gain access to the Redis server within the Vagrant VM. I ended up going this route for my case to connect to Redis inside of a Vagrant VM for local development of an Ember JS app using ember-cli and ember-cli deploy with ember-cli-deploy-redis
You can give a Private IP to the Vagrant box and then access redis using the private IP.
For example, lets say you want to configure 192.168.33.10 as you Vagrant box IP. Simply add this line in Vagrant file.
Vagrant.configure(2) do |config|
config.vm.network 'private_network', ip: '192.168.33.10'
end
From now u can access your vagrant box using 192.168.33.10
I happen to run Redis within a Docker container in addition to the Vagrant setup. Running into this question again and again, I want to a add my findings.
Indeed making sure binding to net devices is to 0.0.0.0 instead of 127.0.0.1 is essential. Since I am running Docker I just had to make sure the container is properly run:
docker run -d redis -p 0.0.0.0:6379:6379 redis
Then, in addition the known Vagrant configuration:
config.vm.network :forwarded_port, guest: 6379, host: 6379
And voilà: running Redis in a Vagrant VM using Docker, able to access it from the Mac OSX host system.
This is without changing the Redis config, since Docker takes care of this.

Using gitlab-vagrant-vm from OSX host

I followed the instructions here and was able to succesfully (I think) install the gitlab vagrant virtual machine on OSX 10.8 using virtualbox.
I can do vagrant up to get the VM running, and everything seems to work fine. After that I can do vagrant ssh without a problem. Also, after sshing into the VM I was able to do bundle exec rake gitlab:test, which completed with results being 1584 examples, 0 failures.
I would like to see the gitlab web interface from my OSX host machine. I thought I could just direct my browser to the IP indicated in the VagrantFile (http://192.168.3.14), but that didn't work.
Any ideas?
Also any other usage tips for this setup would be appriciated (things like where the repositories are stored on my host machine so I can back them up, if anyone set the gitlab-vagrant-vm up for external access from either another computer on the network or a remote source, ect.)
You have to connect a second interface for vagrant. To do this you've to edit the VagrantFile.
For example if you want to conenct to the host wifi add the following line after 192.168.3.14
config.vm.network :bridged, bridge: "en0: Wi-Fi (AirPort)"
You also can bridge to the ethernet interface. Use ifconfig on the host machine to determine the right interface. After that the dyndns-server of the host network will assign an IP to the Vagrant-Box. Then you can access GitLab on that IP.
Did you actually start the server? You can do that with
bundle exec foreman start -p 3000
This will start the server on port 3000, you would then access it from the host with
http://192.168.3.14:3000/
Hope this helps,
Chris

Resources