本文介绍了从 docker 容器内部访问 vagrant VM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在使用 Vagrant/Virtualbox 的 2 个 centos VM 和使用 docker for mac 的 docker 容器之间设置网络.他们都需要能够相互访问.

How do I setup a network between 2 centos VM's using Vagrant/Virtualbox and a docker container using docker for mac. They all need to be able to access each other.

目前我无法从 docker 容器中访问虚拟机.

Currently I'm not able to access the vm's from within a docker container.

流浪设置:

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 "private_network", ip: "192.168.50.4"
   end

   config.vm.define "test" do |test|
      test.vm.box = "centos/7"
      test.vm.provider "virtualbox"
      test.vm.hostname = "server"
      test.vm.network "private_network", ip: "192.168.50.5"
   end
end

虚拟机可以互相访问,但是docker容器不能访问虚拟机

The vm's can access each other but the docker container can't access the vm's

docker network create -d bridge --gateway=192.168.50.1 --subnet=192.168.50.1/24 mybridge
docker run --network=mybridge alpine ping 192.168.50.4
=> not able to connect

推荐答案

您需要将运行 docker 容器的网络和引导 vagrant VM 的网络连接到主机上的同一桥接设备:

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. 创建 docker 网络:

1. Create docker network:

docker network create -d bridge --gateway=192.168.50.1
--subnet=192.168.50.1/24 mybridge

Docker 在主机上创建名为 br- 的网桥:

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. 将 vagrant VMs 网络连接到同一个桥接设备:

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. 启动虚拟机:

$ vagrant up

4.mybridge 网络中启动 docker 容器:

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

这篇关于从 docker 容器内部访问 vagrant VM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 22:14