Through the previous article, we can solve the communication problem within the same container. It is the simplest solution for everyone to use the same bridge network. But what should we do if our container needs to access the host?
In real scenarios, we usually deploy all microservices into containers, but for functions that require extreme stability, such as databases, we need to deploy them on the host machine. Don’t ask me why I don’t buy a cloud database. Asking is just poor. . In fact, it is very simple to solve this problem. We only need to be guided by the principles of bridge networks. A bridged network is equivalent to the host computer acting as a switch host, and then creating a new network segment, and the host computer is the allocator of this network segment, which is called a gateway. Friends who know the network all know that the IP of our router or switch is generally The [***.***.***.1] of this IP is very simple at this time. Our host is the [***.***.***.1] of the current bridged network. .
1. View my current bridged network
[zcsjw_use@iZ8vb3lp570ckrtrhp6f42Z docker]$ docker network ls
NETWORK ID NAME DRIVER SCOPE
965f410854ce bridge bridge local
afdb8db9805c docker_default bridge local
8a85f0b982ae docker_work3 bridge local
ca1da99cf73f host host local
9acaf4ecacea none null local
654c85f7c6c8 product_default bridge local
2. View the network segment of the bridged network
[zcsjw_use@iZ8vb3lp570ckrtrhp6f42Z docker]$
[zcsjw_use@iZ8vb3lp570ckrtrhp6f42Z docker]$ docker inspect docker_work3
[
{
"Name": "docker_work3",
"Id": "8a85f0b982aed1943756795a6023f599d05b4f071426e6ebf33d37ad7db26b02",
"Created": "2022-10-08T10:01:16.627697693+08:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "172.18.0.0/16",
"Gateway": "172.18.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"4c76545ed4d82ec1f848935e3d8293d838dc8b2f2bbba5d758b6cdbe201cf774": {
"Name": "prod-nacos",
"EndpointID": "bb7df3f003327aca8bfd0e19d8e8627e41e0b5cbe3eb2ca70d63ccbdba925685",
"MacAddress": "02:42:ac:12:00:08",
...................................
...................................
...................................
...................................
...................................
...................................
后面还有十几个服务就不放出来了
}
3. Do your own business after obtaining the IP of the host machine.
"Gateway": "172.18.0.1"
Call it a day, I wish you all can learn new knowledge every day! !