我有以下場景 - 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 ~]$ 捲曲http://172.17.0.2:80 -> 這是容器IP - 不透過瀏覽器打開
你好 Docker Swarm!
在測試模式下運作。服務者:be93b0268b1d
[vagrant@docker1 ~]$ 捲曲http://192.168.99.101:8080 -> 這是應該透過瀏覽器開啟的 Docker 主機 IP - 也沒有成功
你好 Docker Swarm!
在測試模式下運作。服務者:be93b0268b1d`