現(xiàn)象:
宿主機(jī)和docker內(nèi)部能互相訪問非常正常,但docker內(nèi)部訪問外部網(wǎng)絡(luò)內(nèi)網(wǎng)其中一個網(wǎng)段172.18.0.x則無法訪問。
排查
由于docker是精簡過的系統(tǒng),需另外安裝網(wǎng)絡(luò)相關(guān)命令
首先更新apt-get,否則在apt-get install 命令時會報E: Unable to locate package xx錯誤
apt-get update
安裝網(wǎng)絡(luò)工具
apt-get install -y net-tools
安裝ping和telnet
apt-get install -y iputils-ping
apt-get install -y telnet
查看本機(jī)ip和網(wǎng)關(guān),可用ifconfig和hostname -i命令
ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.18.1.2 netmask 255.255.255.0 broadcast 172.18.1.255
ether 02:42:ac:14:01:02 txqueuelen 0 (Ethernet)
RX packets 224 bytes 20754 (20.2 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 226 bytes 1617336 (1.5 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
果然ip為172.18.0.x,與現(xiàn)在外部內(nèi)網(wǎng)其中一段Ip重復(fù)了,但是并沒有看到網(wǎng)關(guān),再在cmd里通過docker network命令確認(rèn)一下
docker network ls
NETWORK ID NAME DRIVER SCOPE
f2d743a9d1d7 bridge bridge local
aa072983972b host host local
3689e62360ba none null local
0c92c70dfedd webcloudapi_default bridge local
然后查詢該網(wǎng)絡(luò)詳細(xì)
docker network inspect webcloudapi_default
[
{
"Name": "webcloudapi_default",
"Id": "0c92c70dfeddaca8722c5278831f07cb1555be23fdc74b8536ad2017eeb4ca3e",
"Created": "2023-08-31T01:23:30.156696Z",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "172.18.0.0/16",
"Gateway": "172.18.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {},
"Options": {},
"Labels": {
"com.docker.compose.network": "default",
"com.docker.compose.project": "webcloudapi",
"com.docker.compose.version": "2.20.2"
}
}
]
看到ip和網(wǎng)關(guān)都為172.18.0.x的,這時候確認(rèn)問題,可以著手調(diào)整了。
解決方案
通過docker compose在部署的時候更改默認(rèn)ip和網(wǎng)關(guān)即可。
webapi-compose.yml
version: '3'
networks:
test-net:
ipam:
config:
- subnet: 172.20.1.0/24
gateway: 172.20.1.1
services:
webcloudapi:
image: webcloudapi
container_name: webcloudapi
hostname: webcloudapi
ports:
- 20020:80
restart: always
networks:
test-net:
ipv4_address: 172.20.1.2
重新運(yùn)行部署此yml文件(注意,這個操作會將原容器更改過的內(nèi)容,包括新下載的命令軟件全部重置)
docker compose -p webcloudapi -f webapi-compose.yml up -d
這時候再用api-get重新按上述步驟下載網(wǎng)絡(luò)相關(guān)命令查看,ip和網(wǎng)關(guān)都變成172.20.1.x網(wǎng)段了,說明設(shè)置成功,但ping 172.18.0.x仍然無法連通,奇怪,后來終于發(fā)現(xiàn)問題所在,是之前部署的docker network配置還存在導(dǎo)致的問題
docker network ls
NETWORK ID NAME DRIVER SCOPE
f2d743a9d1d7 bridge bridge local
aa072983972b host host local
3689e62360ba none null local
0c92c70dfedd webcloudapi_default bridge local
d0916e722225 webcloudapi_test-net bridge local
上面的webcloudapi_default是原來的,webcloudapi_test-net是新建的,因此只要刪除原來的即可。文章來源:http://www.zghlxwxcb.cn/news/detail-721149.html
docker network rm webcloudapi_default
重啟容器后,終于ping通了,至此問題解決。文章來源地址http://www.zghlxwxcb.cn/news/detail-721149.html
到了這里,關(guān)于docker內(nèi)部ip與內(nèi)網(wǎng)其它ip網(wǎng)段沖突導(dǎo)致無法訪問的解決方法的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!