Docker——跨主机通信
前面内容我们介绍了docker的网络方式,下面我们来具体实验一下
1. 端口映射(-p /-P)
192.168.137.139和192.168.137.141
139:
编辑首页
[root@Anolis ~]# echo 139 > index.html
启动容器
[root@Anolis ~]# docker run -d --name nginx-139 -v /root/index.html:/usr/share/nginx/html/index.html -p 8080:80 nginx
49c14f858ecd1339e98acd4fad714342eaaacfd1c73e3ef23f1b6e3ac56c2d05测试访问
[root@Anolis ~]# curl 192.168.137.139:8080
139
[root@Anolis ~]# curl 192.168.137.141:8080
141
141:
[root@Anolis2 ~]# echo 141 > index.html
[root@Anolis2 ~]# docker run -d --name nginx-141 -v /root/index.html:/usr/share/nginx/html/index.html -p 8080:80 nginx
cb12993a31e1776a91cb1b642c5371d19c5afd2af20d8b771688dcce1f62e991
[root@Anolis2 ~]# curl 192.168.137.141:8080
141
[root@Anolis2 ~]# curl 192.168.137.139:8080
139
2. 跨主机 bridge 网桥(自定义网络+静态路由)
自定义 Bridge + 宿主机路由,容器用 IP 直接互通(同局域网首选)
固定网段
- 139 容器网段:
172.20.0.0/24 - 141 容器网段:
172.21.0.0/24
第一步:两台分别创建自定义 Bridge
139:
[root@Anolis ~]# docker network create --subnet 172.20.0.0/24 my-bridge-139
52bcc12dc6154c5fc0820c2a31f90f578fa4e3c7bc9b5b3ad68754ddd038adc3
141:
[root@Anolis2 ~]# docker network create --subnet 172.21.0.0/24 my-bridge-141
76ff95406838c4c84f45304daf446349a0c8646ce78814f25b47deee936da4a1
第二步:两台添加跨主机路由(核心!)
139:
[root@Anolis ~]# ip route add 172.21.0.0/24 via 192.168.137.141
告诉机器:访问172.21.0.0网段(141的容器),走192.168.137.141
141:
[root@Anolis2 ~]# ip route add 172.20.0.0/24 via 192.168.137.139
告诉机器:访问172.20.0.0网段(139的容器),走192.168.137.139
第三步:两台启动容器加入自定义网桥
139:
[root@Anolis ~]# echo bridge-139 > index.html
[root@Anolis ~]# docker run -d --name test-139 --network my-bridge-139 -v /root/index.html:/usr/share/nginx/html/index.html nginx
74f8df35f32938622f046d2779a037d2ccec3a81d04391c123972bcdebc98f09
141:
[root@Anolis2 ~]# echo bridge-141 > index.html
[root@Anolis2 ~]# docker run -d --name test-141 --network my-bridge-141 -v /root/index.html:/usr/share/nginx/html/index.html nginx
47c45c32bc94271aab01735a59e42bbb80087e2ed61f5f8cf6d667b6fb932cb2
测试通信
docker exec -it test-139 curl 172.21.0.2
docker exec -it test-141 curl 172.20.0.2
3. Overlay 网络(Docker 原生跨主机方案)
第一步:搭建Swarm 集群
139:初始化管理节点
[root@Anolis ~]# docker swarm init --advertise-addr 192.168.137.139
Swarm initialized: current node (41i5kevnmmkxt5s58sya9qbsg) is now a manager.To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-0a9mgair2vscjt9fibi6fzg3p834zd2nah4dh0hm0nv148iav9-5vtnv2box2c6n5wjzre2uk9si 192.168.137.139:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
141:
[root@Anolis2 ~]# docker swarm join --token SWMTKN-1-0a9mgair2vscjt9fibi6fzg3p834zd2nah4dh0hm0nv148iav9-5vtnv2box2c6n5wjzre2uk9si 192.168.137.139:2377
This node joined a swarm as a worker.
ps:若已经加入swarm,用docker swarm leave --force或者docker swarm leave退出
第二步:创建全局 Overlay 网络(仅 139 执行,管理节点)
[root@Anolis ~]# docker network create -d overlay --attachable my-overlay
0q7jxeisjirdqpgmu57i7gnfd
创建跨主机Overlay网络,所有节点自动同步
第三步:两台启动容器加入 Overlay 网络
139:
[root@Anolis ~]# docker run -d --name overlay-139 --network my-overlay nginx
2e43f77df522980ae2afb6cd1d3910bc1f926978e6f4916fefe1e94e83a2ab59
141:
[root@Anolis2 ~]# docker run -d --name overlay-141 --network my-overlay nginx
db994a355bae2c83eef48022c2cc2daf65594633dfe0f796481a073b32d702ff
测试:
141:通过容器名或者ip访问
[root@Anolis2 ~]# docker exec -it overlay-141 curl overlay-139
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, nginx is successfully installed and working.
Further configuration is required for the web server, reverse proxy,
API gateway, load balancer, content cache, or other features.</p><p>For online documentation and support please refer to
<a href="https://nginx.org/">nginx.org</a>.<br/>
To engage with the community please visit
<a href="https://community.nginx.org/">community.nginx.org</a>.<br/>
For enterprise grade support, professional services, additional
security features and capabilities please refer to
<a href="https://f5.com/nginx">f5.com/nginx</a>.</p><p><em>Thank you for using nginx.</em></p>
</body>
</html>
| 方式 | 复杂度 | 适用场景 | 通信方式 |
|---|---|---|---|
| 端口映射 | 低 | 临时测试、单端口服务 | 宿主机 IP + 端口 |
| 跨主机 Bridge | 中 | 同局域网小集群 | 容器 IP 直连(需配路由) |
| Overlay 网络 | 低 | 生产环境、多主机集群 | 容器 IP / 容器名直连 |
更多推荐

所有评论(0)