How to get two vagrant boxes to talk to each other?

瘦欲@ 提交于 2020-01-03 04:39:07

问题


Let's say I make two vagrant boxes, foo and bar:

$ mkdir -p foo bar
$ pushd foo; vagrant init hashicorp/precise32; vagrant up; popd
$ pushd bar; vagrant init hashicorp/precise32; vagrant up; popd

Now let's say I start an HTTP server on foo:

$ cd foo
$ vagrant ssh -c 'python -m SimpleHTTPServer 8080

My question is, how can I get bar to communicate (e.g. via curl) with foo?

$ cd bar
$ vagrant ssh -c 'curl http://????'

回答1:


Although the question does not make it clear, I think this older question is asking:if the *same* development machine is running two vagrant instances, how can an app running onfoofetch data from a url onbar`.

If so, I ran into this recently for my two Rails apps(each running in a separate vagrant instance on the same development machine).

The two-part 'trick' if foo wants to fetch data from bar, is:

1) each Vagrant files needs a line:

config.vm.network :private_network, ip: PVT_NETWORK

where PVT_NETWORK is a local IP, is different for each Vagrant file, and probably needs to be in the same subnet. For example PVT_NETWORK might be 192.168.50.50 (foo) and 192.168.50.51 (bar)

2) foo accesses bar via the PVT_NETWORK IP address not the "real" IP you would use with a web browser.

In my Rails example, I also have each app running on a different port, so foo is on localhost:3000 and bar is on localhost:3001, so foo would access a url on bar via

http://192.168.50.51:3001/some_url



回答2:


If the two servers are in the same network and no ACL between them (local boxes) they can communicate with each other after configuring the network.

configure the vagrant file:

# Disable automatic box update checking. If you disable this, then
# boxes will only be checked for updates when the user runs
# `vagrant box outdated`. This is not recommended.
# config.vm.box_check_update = false

# 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.
# config.vm.network "forwarded_port", guest: 80, host: 8080

# Create a private network, which allows host-only access to the machine
# using a specific IP.
# config.vm.network "private_network", ip: "192.168.33.10"

# Create a public network, which generally matched to bridged network.
# Bridged networks make the machine appear as another physical device on
# your network.
# config.vm.network "public_network"


来源:https://stackoverflow.com/questions/30492761/how-to-get-two-vagrant-boxes-to-talk-to-each-other

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!