Vagrant Machines - Errno::ECONNREFUSED (Failed to open TCP connection to localhost:3001 - vagrant

I am currently setting up a development environment whereby there are two vagrant machines. The first hosts a website etc while the second hosts an API which the website calls. Currently my vagrantfiles are as follows:
API
Vagrant.configure("2") do |config|
config.vm.box = "hashicorp/precise64"
config.vm.network "forwarded_port", guest: 3001, host: 3001
end
Website
Vagrant.configure("2") do |config|
config.vm.box = "hashicorp/precise64"
config.vm.network "forwarded_port", guest: 3000, host: 3000
end
When i attempt to load the site which makes a call to the API I get:
Errno::ECONNREFUSED (Failed to open TCP connection to localhost:3001 (Connection refused - connect(2) for "localhost" port 3001)):
I understand there are multi-machine approaches but as I already have an seperate environment set up for each project I was wondering if there is any way for me to allow the connection between the two. I have looked into changing the hosts file on the vagrant machine hosting the site but not sure what I should do.

You need to put both VMs into the same private network and Website VM will be able to reach API VM via it:
config.vm.network "private_network", ip: "<ip_address>"
For example, for your case:
API VM:
Vagrant.configure("2") do |config|
config.vm.box = "hashicorp/precise64"
config.vm.network "forwarded_port", guest: 3001, host: 3001
config.vm.network "private_network", ip: "192.168.200.3"
end
Website VM:
Vagrant.configure("2") do |config|
config.vm.box = "hashicorp/precise64"
config.vm.network "forwarded_port", guest: 3000, host: 3000
config.vm.network "private_network", ip: "192.168.200.2"
end
Now, Website VM can reach API VM on 192.168.200.3:3001. And of course you can reach Website VM on 127.0.0.1:3000 and API VM on 127.0.0.1:3001 from your host machine.

Related

vagrant port forwarding guest and host port messed up

I'm using a vagrant trusty64 box with nginx, flask, gunicorn and port forwarding is not working as expected
In the vagrant file, I have:
config.vm.network "forwarded_port", guest: 8090, host: 3100
config.vm.network "private_network", ip: "10.10.1.10"
On the box, I have run:
gunicorn myprj:app -b 10.10.1.10:8090
find nothing on host machine with http://10.10.1.10:3100
trying curl -v http://10.10.1.10:3100 gives the following output:
connect to 10.10.1.10 port 3100 failed: Connection refused
Reachable with guest port on host machine http://10.10.1.10:8090
I am new to the vagrant, did I miss/mess to Vagrant file.
Complete Vagrant file:
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/trusty64"
config.vm.box_check_update = false
config.vm.network "forwarded_port", guest: 8090, host: 3100
config.vm.network "private_network", ip: "10.10.1.10"
config.vm.synced_folder ".", "/vagrant_data"
config.vm.provider "virtualbox" do |vb|
vb.memory = "1024"
vb.name = "lopamudra_dev"
end
config.vm.provision "shell", inline: <<-SHELL
# Upgrading the environment
apt-get update
apt-get upgrade
# Installing nginx + uwsgi + flask
apt-get install -y python-pip python-dev nginx
pip install uwsgi flask gunicorn
SHELL
end
You are forwarding container 8090 to hostIP:3100 (localhost:3100)
At the same time you are creating a IP to access your container with 10.10.1.10 and you container has 8090 exposed, thats why you can acces it on 10.10.1.10:8090
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine. In the example below,
# accessing "localhost:8080" will access port 80 on the guest machine.
# NOTE: This will enable public access to the opened port
config.vm.network "forwarded_port", guest: 8090, host: 3100
# Create a private network, which allows host-only access to the machine
# using a specific IP.
config.vm.network "private_network", ip: "10.10.1.10"

CouchDB on Vagrant VM (Scotch box)

I'm trying to set up CouchDB (and ultimately use PouchDB) on a Scotch Box VM. The VM runs fine, and includes port forwarding for port 5849 by including the code below:
Vagrant.configure("2") do |config|
config.vm.box = "scotch/box-pro"
config.vm.hostname = "scotchbox"
config.vm.network "forwarded_port", guest: 80, host: 8080
config.vm.network "forwarded_port", guest: 5984, host: 5984
config.vm.network "forwarded_port", guest: 3000, host: 3000
config.vm.network "private_network", ip: "192.168.33.10"
The VM runs fine. localhost:8080 will load the PHP home page, and localhost:3000 will load a node script if I have the node server running, but localhost:5984 only returns an empty response when loaded from the browser or from the host machine command line using curl curl: (52) Empty reply from server.
When I have used vagrant ssh to access the VM, I can use curl localhost:5984 to obtain {"couchdb":"Welcome","uuid":"9cabeb8f66947adabe9443594aa7f69c","version":"1.6.0","vendor":{"version":"15.10","name":"Ubuntu"}} as expected.
Here is the guide I've been referring to: https://pouchdb.com/guides/setup-couchdb.html
Additional info: When I go to 192.168.33.10:5984 (instead of using the localhost port forwarding), the result is a refused connection.
Any suggestions as to what my issue might be? I had thought it was a forwarding issue, but the ports 8080 and 3000 work fine, and going to the IP:5984 doesn't work so it makes me wonder. I also thought maybe the service isn't running, but doing ssh on the VM and running curl seems to indicate that it is in fact running.
Thanks!
By default CouchDB bound to a localhost address 127.0.0.1 and you need to re-bind it to 0.0.0.0 to get it accessible from outside of Vagrant box. To do that you need to change parameter bind_address in [httpd] section of default.ini config file or add the same as override in local.ini config file.

Files created in Vagrant centos/7 do not appear in Windows [duplicate]

This question already has answers here:
Vagrant Synced Folder without reload?
(2 answers)
Closed 5 years ago.
I have primarily used Ubuntu boxes in Vagrant and in those any files created within the box have been successfully synced to Windows also. Usually things like git pull.
However, I now have centos/7 and cannot get any files to sync to Windows. If you create a file in /vagrant it never appears anywhere in Windows - not under C:\cygwin64\ nor C:\vm\machine. When I vagrant reload the box, all the files created inside the box are gone, except configuration files eg. /etc/httpd/conf/httpd.conf. Any changes in them are still present upon vagrant reload and also vagrant halt; vagrant up.
Here's the Vagrantfile:
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
config.vm.box = "centos/7"
config.vm.network "forwarded_port", guest: 80, host: 8086, host_ip: "127.0.0.1"
config.vm.network "forwarded_port", guest: 3306, host: 3386, host_ip: "127.0.0.1"
config.vm.synced_folder "./", "/vagrant"
# I have also tried the following:
# config.vm.synced_folder "C:/vm/machine", "/vagrant"
# config.vm.synced_folder "./", "/vagrant", type: "rsync", disabled: false
end
When doing vagrant up, both methods for synced_folder display this:
==> default: Rsyncing folder: /cygdrive/c/vm/machine/ => /vagrant
I have tried doing vagrant up from both cmd.exe and also from the Cygwin64 Terminal (under /c/vm/machine). Neither of them create files to the Windows directory, which is in C:\vm\machine.
What do I need to do to make files created in the box also sync to Windows?
When I try the same in an Ubuntu box, the file appears in Windows and does not disappear:
This is the Vagrantfile of the Ubuntu box:
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
config.vm.box = "v0rtex/xenial64"
config.vm.network "forwarded_port", guest: 80, host: 8083, host_ip: "127.0.0.1"
config.vm.network "forwarded_port", guest: 443, host: 8443, host_ip: "127.0.0.1"
config.vm.network "forwarded_port", guest: 3306, host: 3303, host_ip: "127.0.0.1"
config.vm.network "forwarded_port", guest: 5000, host: 5003, host_ip: "127.0.0.1"
config.vm.network "forwarded_port", guest: 8000, host: 8003, host_ip: "127.0.0.1"
config.vm.network "forwarded_port", guest: 9090, host: 9093, host_ip: "127.0.0.1"
config.vm.network "forwarded_port", guest: 9091, host: 9193, host_ip: "127.0.0.1"
end
Upon vagrant up for the Ubuntu box, I see this:
==> default: Mounting shared folders...
default: /vagrant => C:/vm/ubu1604-64
The solution is as follows:
Change the Vagrantfile synced_folder part to this:
config.vm.synced_folder "./", "/vagrant", type: "virtualbox", disabled: false
And then run this command in the directory where the Vagrantfile is:
vagrant plugin install vagrant-vbguest
Then do either vagrant reload if it was already running, or vagrant up otherwise.
It will run for quite a while - for me it took about 7 minutes altogether. However, it is a one-time install, so all future vagrant up's will take a normal amount of time.
After that, the files created in the /vagrant directory will also appear in Windows C:\vm\machine (for example) and will persist between Vagrant sessions, ie. vagrant halt + vagrant up

Vagrant cURL works but Mozilla doesn't

I have the following issue. I just configured Vagrant box on Windows 7. This is my first time to use Vagrant. Guest port 80, host 8008(8080 is in use). It's all working well but http://localhost:8008/ in Mozilla dosen't respond in any way. From the virtual mashine curl 'http://localhost:80' works as expected. From the local mashine curl -v "http://localhost:8008/"as expected too. I tryed different browsers and firewall off - notting. Restart win, restart browser, clear Mozilla cash - all the same. The server is Ubuntu.
This is the vagrantfile:
Vagrant.configure("2") do |config|
config.ssh.forward_agent = true
config.vm.box = "ubuntu/trusty64"
config.vm.network :private_network, ip: "192.168.50.4"
config.vm.network "forwarded_port", guest: 80, host: 8008
config.vm.provision :shell, :path => File.join( "provision", "provision.sh")
end
I'm probably doing something wrong but I'm not sure what. Please for any suggestions what that may be.
you're using both private IP and forward port - use one or the other
Using forwarded port
Vagrant.configure("2") do |config|
config.ssh.forward_agent = true
config.vm.box = "ubuntu/trusty64"
config.vm.network "forwarded_port", guest: 80, host: 8008
config.vm.provision :shell, :path => File.join( "provision", "provision.sh")
end
and from your host you will be able to access http://localhost:8008/
using private IP
Vagrant.configure("2") do |config|
config.ssh.forward_agent = true
config.vm.box = "ubuntu/trusty64"
config.vm.network :private_network, ip: "192.168.50.4"
config.vm.provision :shell, :path => File.join( "provision", "provision.sh")
end
and from your host you will be able to access http://192.168.50.4/

Changing domain mapping on Mac OSX etc/hosts to Vagrant Virtual Machine

I've been trying to change the mapping for a domain and forward it to a Vagrant machine that I've forwarded a port to.
The config in Vagrantfile:
Vagrant.configure("2") do |config|
config.vm.network :forwarded_port, host: 4567, guest: 80
end
I've updated /etc/hosts on my Mac OSX with the following:
127.0.0.1:4567 example.com
Yet when I access from my browser example.com nothing changes, I have also tried to map example.com to go to the Google IP without any luck.
What am I overlooking?
I would just add a static ip, or try removing the port number:
Vagrant.configure("2") do |config|
config.vm.network "private_network", ip: "192.168.50.4"
end
Then in your hosts file change it to read:
192.168.50.4 example.com
More info is available here:
https://docs.vagrantup.com/v2/networking/private_network.html

Resources