Boot2Docker private network setup - vagrant

How can I do the private networking for the boot2docker docker container ?
For example, If I have a webapp, I can do the following in Vagrantfile
myapp1.vm.network "private_network", ip: "1.2.3.4"
myapp2.vm.network "private_network", ip: "1.2.3.5"
myapp3.vm.network "private_network", ip: "1.2.3.6"
Then I can use my browser to access my webapp at
http://1.2.3.4
http://1.2.3.5
http://1.2.3.6
How can I achieve the same result in docker easily ?
I also looked at How to expose docker container's ip and port to outside docker host without port mapping?
But in my boot2docker1.3, it said the interface eth0:1 does not exist
I looked at https://docs.docker.com/articles/networking
The tutorial does not work for boot2docker in mac.
Any help would be appreciated, thankyou!

I believe the instructions here, linked from this question give what you are looking for. The key is this line:
sudo route -n add 172.17.0.0/16 172.16.0.11
which tells your Mac how to route to the private network inside the VirtualBox VM that the Docker containers are on. (Obviously the specific addresses can change for your specific situation)
This still doesn't give you the ability to assign specific IP addresses to specific containers; as I said an add-on like weave can do that. (note: I work on weave)
You may also like this article which gives a beginner's overview of how Boot2Docker runs and illustrates how you have IP addresses inside the VirtualBox VM, and also an IP address of that box as a whole.

Related

How to avoid "Incoming connections" popup in PhpStorm for several vagrant servers

I have two vagrant servers and according PhpStorm projects for them. I configured xdebug for both as well. Debugging works. But when I added the second server I have a popup "Incoming connections" with options of my projects. It asks which project xdebug should relate the incoming request to. I found the issue. It happens because both vagrant machines have the same ip. And PhpProjects also have the same ip for projects. So it is quite naturally that PhpStorm can not define a write project for debugging. But should I do. First, I tried to change vagrant machine ip:
sudo ifconfig enp0s3 192.168.0.253 netmask 255.255.255.0
This command hangs on. Also tried to add a config in /etc/network/interfaces:
iface enp0s3 inet static
address 192.168.0.253
netmask 255.255.255.0
No success too. I searched for vagrant way to change ip, but nothing. I also tried to find a way to tell xdebug which ip it should send to PhpStorm but how?
It's unlikely that only I have this problem but did not find any relevant information about this issue.
The sulution turned out to be easy. Virtual box has a specific option for a machine ip. One can configure it as follows (Vagrantfile):
config.vm.provider "virtualbox" do |vb|
vb.customize ['modifyvm', :id, '--natnet1', '192.168/16']
This will setup an ip different from the default one 10.0.2.15. Basically, instead of 192.168/16 could be any valid ip you wish. But following given example a random ip will be set for the machine. This ip could be found by connecting to a machine (ssh) and checking ip configuration as any machine of your OS. In ubuntu ifconfig works.
Then you need to create a server in PhpStorm with obtained ip in settings Languages and frameworks > PHP > Servers. Most likely you will also need to setup path mapping for the server.

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.

How can I access a vagrant guest from another virtualbox guest?

The scenario is that my dev environment is on a Vagrant box on my laptop (host) and I would like to do browser testing in a vitualbox vm, so I need to see one vm from another.
The vagrant box's port is :8080 which is forwarded to the host on the same port :8080. So I can see the server from the host at localhost:8080
Which address should I be using for the browser testing vm?
The testing vm's default gateway?
The vagrant vm's ip?
The host's virtual network ip?
And should I be using a NAT or host only adapter on the browser testing vm?
That makes for a lot of combinations, all of which I believe I have tried. What else do I need to understand here?
In your use case, you should be using Bridged networking (Public Network in Vagrant). If the VMs reside on the same host, you can even use internal (Private Network in Vagrant).
If using Public Network, the VM's 2nd NIC will be able to obtain an IP address from the DHCP server in your network (e.g. your home router).
Simply add the following code block in your Vagrantfile and do a vagrant reload
Vagrant.configure("2") do |config|
config.vm.network "public_network"
end
You should be able to get the IP address by using vagrant ssh and ifconfig / ip addr show.
In case you don't want to go with public_network just like me then you should do the steps below using private_network:
Open Vagrantfile from your project root
Search for config.vm.network
Add this line config.vm.network "private_network", ip: "192.168.33.10". Remember this is not the IP of your base machine it's a virtual-box IP address and your machine IP should be different. You can say it's a fake IP address so change it to anything else like 192.168.30.20.
Reload your vagrant using vagrant reload.
Now go to your other virtual guest in my case it's the Windows Guest 2. My base is Linux Mint Vagrant box is on Ubuntu Guest 1. Open C:\Windows\System32\drivers\etc\hosts file as admin and do the above IP's entry in there like 192.168.33.10 local.youralias.com. And save the file, after that you can now browse the site now at http://local.youralias.com/.
In case your guest 2 is also Linux just edit this file sudo vi /etc/hosts, and add this line at top of it 192.168.33.10 local.youralias.com. Now save and exit and browse the URL :)
Enjoy! Happy coding.
Adding to accepted answer, you can actually set IP and specify which network interface to use.
My setup on linux box via wifi and static IP:
You can find your wifi interface name by running ifconfig command.
Vagrant.configure("2") do |config|
config.vm.network "public_network", :bridge => 'wlp8s0', ip: "192.168.1.199"
end
This may have many source cause. In my case, I use vagrant fedora boxe.
I tried:
First using the private_network that I attached to a host only adapter and launched httpd service to test the connection between guest and host
config.vm.network "private_network", type: "dhcp", name: "vboxnet2"
config.vm.network "forwarded_port", guest:80, host:7070
but I was not able to ping my guest machine from the host and could no telnet the httpd service opened
Second using public_network and launched httpd service to test connectivity
config.vm.network "public_network", bridge: "en0: Wi-Fi (AirPort)", use_dhcp_assigned_default_route: true
I could ping my guest from my host but I could not telnet the httpd service.
For this two use case, the issue was that the port 80 on the fedora guest host was blocked by the firewall. Here is what fixed the issue and get all working for both privat_network and public_ntwork:
firewall-cmd --permanent --add-port 80/tcp #open the port permanently
firewall-cmd --zone=public --permanent --add-service=http
firewall-cmd --list-port # list to check if the port was opened
systemctl stop firewalld # stop and open the firewall service
systemctl start firewalld
Old question, new answer: [disclaimer: i am not a vagrant expert]
both solutions might work but the solution in the "vagrant way of thinking" is that some component in your guest (rinetd?) should forward any requests to unknown ports to the host. From the host the request could then be mapped (via vagrant port forwarding) to a services that is running in the other guest.
So, to resume:
1.in guest-1 we do localhost:1234. Guest-1 will detect that this port is not available and forward to host
2. the host will check the vagrant port forwarding and forward to guest-2
3. in guest-2 we have some nice service listening to post 1234
4. done.

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

Setting a VM's mac address in Vagrant

The documentation lists that the mac address of a VM can be set in the Vagrantfile, however everything I add seems to end up being a syntax error. Anyone successfully done this?
I used this:
config.vm.network :bridged , :mac => "080027XXXXXX"
and got what I wanted.
The docs are unclear on what the syntax for the options hash were, and there seemed to be no example on what this should look like. So, here it is! Bridged with a mac address (edited of course). This brings up eth1 with the mac specified, which makes my DHCP server happy, and gives it a proper fqdn on my network.
This is an old question, but I had the same issue just now. Vagrant documentation v2 still seems incomplete. In the end I used this line in the Vagrantfile with vagrant 1.2.7:
config.vm.network "public_network", :bridge => 'enp4s0', :mac => "5CA1AB1E0001"
This:
sets the host interface named 'enp4s0' as the bridge interface,
which as 'eth0' on the guest is then assigned an ip address by the same DHCP the host uses
Also sets 5C:A1:AB:1E:00:01 as the guest's mac address
hmm, the network config didn't help in my case. After defining the MAC Address directly in the Vagrantfile via config.vm.base_mac = "MyEth0MacAddressWithoutSlashes" my machine started =)
On Vagrant version 2.0.1, I write in the Vagrantfile for a private_network (provider = VirtualBox ; version 5.2.0) :
config.vm.network "private_network", ip: "X.X.X.X", mac: "080027xxxxxx"
The information provided below is outdated. As per documentation to allow IP to be assigned via DHCP simply use:
config.vm.network "public_network"
This way you don't need to deal with mac address, it will be generated on its own.
If you need custom mac address attached to the network device then:
config.vm.network "public_network", :mac=> "080027xxxxxx"

Resources