gpt4 book ai didi

docker - 从Docker容器中访问 Vagrant 的虚拟机

转载 作者:行者123 更新时间:2023-12-02 20:06:06 25 4
gpt4 key购买 nike

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

目前,我无法从Docker容器中访问虚拟机。

流浪汉设置:

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容器不能访问虚拟机
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容器的网络和将无用的VM引导到主机上同一桥设备的网络:

1. 创建docker网络:

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. 将无用的VM网络连接到同一网桥设备:
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

关于docker - 从Docker容器中访问 Vagrant 的虚拟机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49317065/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com