从docker容器内访问vagrant VMs

时间:2018-01-29 18:02:55

标签: docker networking vagrant

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

目前我无法从docker容器中访问vm。

流浪汉设置:

context

vm可以互相访问,但是docker容器无法访问vm的

holder.imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    try {
                        Bitmap bitmap = ((BitmapDrawable)((ImageView)view).getDrawable()).getBitmap();
                        WallpaperManager wallpaperManager = WallpaperManager.getInstance(context);
                        wallpaperManager.setBitmap(bitmap);

                       Toast.makeText(context, "Wallpaper Set", Toast.LENGTH_LONG).show();

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });

2 个答案:

答案 0 :(得分:9)

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

1。创建docker network:

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

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

$ 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.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容器:

$ 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

答案 1 :(得分:0)

建议的 Vagrantfile 不适用于此环境:

  • 操作系统:Debian Bullseye(测试)
  • 流浪者:2.2.14
  • Vagrant 提供程序:libvirt (KVM)

这是有效的行:

  config.vm.network "public_network", ip: "192.168.50.4", dev: "br-e13b3ccc6691", mode: "bridge", type: "bridge"

我的猜测,因为提供者。