将Vagrant VM和Docker容器连接到同一网桥网络

时间:2018-07-05 07:02:57

标签: docker networking vagrant

我已按照此问题答案的说明将Vagrant VM和Docker容器都连接到同一桥接网络:Access vagrant VMs from inside docker container。网络状态如下:

  • 容器和VM都可以正确ping通。
  • 虚拟机和容器无法连接到使用curl彼此暴露的端口。
  • 虚拟机可以访问Internet,而容器则不能。
  • 我无法通过暴露的端口(无论是通过localhost还是通过容器IP)从主机访问容器。
  • 我可以从主机和网络上正确访问VM公开的端口。

我以这种方式创建了网络:

docker network create -d bridge --gateway=192.168.51.1 --subnet=192.168.51.0/24 my-bridge

Docker容器:

docker run -ti -p 8912:8080 -v ${PWD}:/workdir --network my-bridge --name mycontainer myimage /bin/bash

和Vagrantfile:

config.vm.define "management" do |management|
    management.vm.box = "centos/7"
    management.vm.network "public_network", ip: "192.168.51.50", bridge: "br-4ab95f14d7eb"
    management.vm.hostname = "myhostname"

    management.vm.network "forwarded_port", guest: 32769, host: 32769
    management.vm.network "forwarded_port", guest: 32768, host: 32768
    management.vm.network "forwarded_port", guest: 8080, host: 8081
    management.vm.network "forwarded_port", guest: 8443, host: 8082
    management.vm.network "forwarded_port", guest: 6080, host: 6080
    management.vm.network "forwarded_port", guest: 61880, host: 61880
    management.vm.network "forwarded_port", guest: 6182, host: 6182

    management.vm.provider "virtualbox" do |v|
      v.memory = 8192
      v.cpus = 4
    end

结束

成为br-4ab95f14d7eb桥接网络:

# docker network ls | grep my-bridge
# 4ab95f14d7eb        my-bridge          bridge              local
# brctl show | grep 4ab95f14d7eb
# br-4ab95f14d7eb       8000.02423ffbd644   no  

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,我尝试了一些解决方法,但不得不将Vagrant虚拟化提供程序更改为libvirt。我安装了KVM,Qemu和Libvirt。我用了这个Vagrantfile

Vagrant.configure("2") do |config|
  config.vm.box = "metasploitable3-ub1404"
  config.vm.hostname = "metasploitable3-ub1404"
  config.ssh.username = 'vagrant'
  config.ssh.password = 'vagrant'
  config.vm.network :public_network,
        :ip => '192.168.51.50', 
        :dev => "4ab95f14d7eb", 
        :type => "bridge",
        :mode => "bridge"
end

请注意,此映像已激活了iptables,您应该禁用此服务才能启用与容器的L3通信。

后来您执行了与您相同的步骤:

  1. 创建桥梁

    docker network create -d bridge --gateway = 192.168.51.1 --subnet = 192.168.51.0 / 24 my-bridge

  2. 启动容器(您无需绑定或暴露端口)

    docker run -ti -v $ {PWD}:/ workdir --network my-bridge --name mycontainer ubuntu / bin / bash

  3. 为了测试端口连接,我将mini-httpd软件包安装到了容器中

  4. 启动虚拟机

    无所事事--provider = libvirt

  5. 在容器中启动Web服务器

    mini_httpd -p 4444

  6. 在虚拟机上检查连接

    卷曲192.168.51.2:4444