我有以下场景 - 3x Vagrant VM 从下面的 Vagrantfile 旋转而来,启动并运行(在 Oracle VirtualBox 中可见)。
我登录docker1,网络类型为“NAT”,使用端口转发(Guest:8080,Host:8080)。
我能够从 localhost:8080 & 127.0.0.1:8080 & 127.0.1.1:8080 访问该页面。但是,我无法通过 IP 进行访问(192.168.99.101:8080 为“响应时间过长”且无法访问)。
Vagrant.configure("2") do |config|
config.vm.define "docker1" do |docker1|
docker1.vm.box="xxx/centos-stream-8"
docker1.vm.hostname = "docker1.do1.lab"
docker1.vm.network "private_network", ip: "192.168.99.101"
docker1.vm.provision "shell", path: "docker-setup.sh"
docker1.vm.provision "shell", path: "other-steps.sh"
docker1.vm.provider :virtualbox do |vb|
vb.customize ["modifyvm", :id, "--memory", "1536"]
end
end
config.vm.define "docker2" do |docker2|
docker2.vm.box="xxx/centos-stream-8"
docker2.vm.hostname = "docker2.do1.lab"
docker2.vm.network "private_network", ip: "192.168.99.102"
docker2.vm.provision "shell", path: "docker-setup.sh"
docker2.vm.provision "shell", path: "other-steps.sh"
docker2.vm.provider :virtualbox do |vb|
vb.customize ["modifyvm", :id, "--memory", "1536"]
end
end
config.vm.define "docker3" do |docker3|
docker3.vm.box="xxx/centos-stream-8"
docker3.vm.hostname = "docker3.do1.lab"
docker3.vm.network "private_network", ip: "192.168.99.103"
docker3.vm.provision "shell", path: "docker-setup.sh"
docker3.vm.provision "shell", path: "other-steps.sh"
docker3.vm.provider :virtualbox do |vb|
vb.customize ["modifyvm", :id, "--memory", "1536"]
end
end
end
docker-setup.sh 文件如下:
#!/bin/bash
echo "* Add Docker repository ..."
dnf config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
echo "* Install Docker ..."
dnf install -y docker-ce docker-ce-cli containerd.io
echo "* Enable and start Docker ..."
systemctl enable docker
systemctl start docker
echo "* Add vagrant user to docker group ..."
usermod -aG docker vagrant
other-steps.sh 文件如下:
#!/bin/bash
echo "* Add hosts ..."
echo "192.168.99.101 docker1.do1.lab docker1" >> /etc/hosts
echo "192.168.99.102 docker2.do1.lab docker2" >> /etc/hosts
echo "192.168.99.103 docker3.do1.lab docker3" >> /etc/hosts
echo "* Install Additional Packages ..."
dnf install -y jq tree git nano
echo "* Firewall - swarm - open ports ..."
firewall-cmd --add-port=2377/tcp --permanent
firewall-cmd --add-port=4789/udp --permanent
firewall-cmd --add-port=7946/tcp --permanent
firewall-cmd --add-port=7946/udp --permanent
echo "* Firewall - app - open port 8080 ..."
firewall-cmd --add-port=8080/tcp --permanent
firewall-cmd --reload
网络如下:
[vagrant@docker1 etc]$ docker network ls
NETWORK ID NAME DRIVER SCOPE
f2351cd29061 bridge bridge local
f91a21d83931 docker_gwbridge bridge local
c5fc9aedf330 host host local
oc2i4c2czira ingress overlay swarm
50fe2b72573c none null local
etc/hosts 内容在这里:
[vagrant@docker1 etc]$ cat hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.1.1 docker1.do1.lab docker1
192.168.99.101 docker1.do1.lab docker1
192.168.99.102 docker2.do1.lab docker2
192.168.99.103 docker3.do1.lab docker3
用于启动容器的命令如下:
docker container run -d --name web -p 8080:80 -e APP_MODE='test' swarm-app
我查阅了大量有关相同问题的文章,但仍然找不到解决方案。我想这可能与相关 IP 地址的 DNS 解析有关?
更新:我正在通过 curl 在 CMD 中打开以下地址(端口转发主机:8080,访客:80),如果有任何信息需要提供,请告诉我: `[vagrant@docker1 ~]$ curl http:/localhost:8080-> 无法在浏览器中打开
你好,Docker Swarm!
正在测试模式下运行。服务方:be93b0268b1d
[vagrant@docker1 ~]$ curlhttp://172.17.0.2:80 -> 这是容器 IP - 无法通过浏览器打开
你好,Docker Swarm!
正在测试模式下运行。服务方:be93b0268b1d
[vagrant@docker1 ~]$ curlhttp://192.168.99.101:8080 -> 这是应该通过浏览器打开的 Docker 主机 IP,同样没有成功
你好,Docker Swarm!
正在测试模式下运行。服务方:be93b0268b1d`