How to access Redis running in Vagrant Virtual Machine - macos

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.

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.

How do I connect to a docker container running on a different host on a mac?

I have set up docker on my Mac using Kinematic. I have a mysql container running.
So, the 'schematic', I believe, looks like this: (Mac1 (VirtualBox Docker Host (MySQL Container))).
Mac1 IP: 10.10.10.100
MySQL Container: 192.168.99.100 running on port 32500 (mapped to 3306)
I can access the MySQL server via 192.168.99.100:32500 from Mac1 without any issues.
Now, when I go to Mac2 on my network, I cannot access the MySQL container.
So, Mac2 IP: 10.10.10.200 cannot see 192.168.99.100:32500. Mac2 gets a ping response, but not sure if it is getting all the way to the MySQL Container.
I'm thinking of installing a proxy on Mac1, but wanted to check first.
None of the questions posed here seemed to account for a Kinematic install on a Mac, and the ones referring to boot2docker didn't address the question either.
Any insights would be most welcome! Thanks in advance.
If you are looking for the simplest way to expose a container port to your network, you could map a virtual box port onto the mac1 and access it from outside.

How to route and network Vagrant VM instances?

I have recently started using Vagrant instead of MAMP for some small projects (mainly WordPress) However, decided to try my hand at React and am wondering wether it is possible to use the two together?
At the moment I have a Vagrant setup created via PuPHPet which works fine and runs a local server on a VM which I can access via a URL - local.gethandle.com. I also downloaded a React starter kit as a starting point from a Facebook github page and it requires you to run a server e.g. node server.js which also works and I can access from a separate URL - http://localhost:3000/. But cant seem to access my React application via my Vagrant URL.
How do I combine the two? If at all possible.
Oki doki,
caveat: I've not looked at what kind of VM is provisioned via PuPHPet..
First you'll need to vagrant ssh into your vagrant vm and download the react tools into your vagrant vm
https://docs.vagrantup.com/v2/getting-started/up.html
Second you'll then want to edit your Vagrant file that should be in the directory on your machine where your vagrant settings are, then have a look and add some networking config
https://docs.vagrantup.com/v2/getting-started/networking.html
config.vm.network :forwarded_port, guest: 80, host: 4567
# guest is your machine, host is your vagrant vm
# e.g config.vm.network :forwarded_port, guest: 3000, host: 3000
You probably need to add the port to your firewall:

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

Docker container - how to configure so it gets a viable IP address when running in vagrant?

Docker (www.docker.io) looks terrific. However, after installing VirtualBox, Vagrant
... and finally Docker on a Mac, I'm finding it's not possible to access the service running in the Docker container from another computer (or from a terminal session on the Mac). The service I'm trying to access is Redis.
The problem appears to be that there's no route to the IP address assigned to the Docker container. In this case the container's IP is 172.16.42.2 while the Mac's IP is 196.168.0.3.
A couple notes:
It IS possible to access it - but only from within the VirtualBox session. This can be done using redis-cli -h 172.16.42.2 -p 6379.
I have added "config.vm.network :bridged" to the VagrantFile in an attempt to get the, but that didn't solve the problem.
The VM generated by vagrant is indeed isolated, in order to access it from your host, you can allocate a private network to it.
Instead of doing config.vm.network :bridged, try config.vm.network :private_network, ip: "192.168.50.4", It should do the trick
However, this will only allow you to access the VM itself, not the containers.
In order to do so, when running the container, you can add the -p option
ex: docker run -d -p 8989 base nc -lkp 8989
This will run a netcat listening on 8989 within a container and expose the port publicly. As it is also run with -d, the container will be in detached mode and the only output will be the container's ID
In order to expose the port, Docker do a simple NAT. In order to know the real port, you can
do docker port <ID of the container> 8989
Netcat will be available from the mac at 192.168.50.4:<result>
I just wrote a tutorial of how to use a host-only network and TCP routing to make this pretty easy. This way you don't have to map every specific port.
http://ispyker.blogspot.com/2014/04/accessing-docker-container-private.html
Important points ...
1) Add host-only network to Virtual Box
2) Tell the boot2docker VM to have an adapter on the host-only network
3) Add an IP for the new boot2docker VM host-only networking adapter
4) Route all Mac OS X traffic for the docker container subnet to that boot2docker VM host-only networking IP
Actual steps are on the blog with output so you can compare to what you see as you follow them.
I have installed tomcat from my Dockerfile and forwarded that to 6060 using vagrant`s port forwarding. These are the steps worked for me:
vagrant provision
vagrant up
vagrant ssh
box_name$ docker run -i -t -p 8080:8080 bsb_tomcat6 /bin/bash
Able to see tomcat up & running on localhost:6060, as I have done port forwarding to 6060 in my Vagrantfile
you also can define PRIVATE_NETWORK and FORWARD_DOCKER_PORTS environment variables to access your services that are running in docker containers:
$ vagrant halt
$ export PRIVATE_NETWORK=192.168.50.4
$ export FORWARD_DOCKER_PORTS=1
$ vagrant up
In my case i can access postgres from Mac using
$ telnet 192.168.50.4 49154
to find out actual application port you can use
$ sudo docker port 1854499c6547 5432
0.0.0.0:49154

Resources