简体   繁体   中英

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM