You need to connect network where you run a docker container and a network where you boot vagrant VMs to the same bridge device on your host machine:
1. Create docker network:
docker network create -d bridge --gateway=192.168.50.1
--subnet=192.168.50.1/24 mybridge
Docker creates bridge with name br-<network_id>
on host machine:
$ docker network ls | grep mybridge
e13b3ccc6691 mybridge bridge local
$ brctl show | grep e13b3ccc6691
br-e13b3ccc6691 8000.024277661b29 no
$ ip r s | grep e13b3ccc6691
192.168.50.0/24 dev br-e13b3ccc6691 proto kernel scope link src 192.168.50.1 linkdown
2. Connect vagrant VMs network to the same bridge device:
Vagrant.configure("2") do |config|
config.vm.define "build" do |build|
build.vm.box = "centos/7"
build.vm.provider "virtualbox"
build.vm.hostname = "server-a"
build.vm.network "public_network", ip: "192.168.50.4", bridge: "br-e13b3ccc6691"
end
config.vm.define "test" do |test|
test.vm.box = "centos/7"
test.vm.provider "virtualbox"
test.vm.hostname = "server"
test.vm.network "public_network", ip: "192.168.50.5", bridge: "br-e13b3ccc6691"
end
end
3. Boot VMs:
$ vagrant up
4. Start docker container in mybridge
network:
$ docker run -ti --network=mybridge alpine ping -c2 192.168.50.4
PING 192.168.50.4 (192.168.50.4): 56 data bytes
64 bytes from 192.168.50.4: seq=0 ttl=64 time=0.898 ms
64 bytes from 192.168.50.4: seq=1 ttl=64 time=0.869 ms
--- 192.168.50.4 ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 0.869/0.883/0.898 ms
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…