SoFunction
Updated on 2025-03-09

Docker implements cross-host container communication based on macvlan

Find two test machines:

[root@docker1 centos_zabbix]# docker network ls
NETWORK ID     NAME        DRIVER       SCOPE
19ac9a55bedb    bridge       bridge       local
0a3cbfe2473f    host        host        local
aab77f02a0b1    none        null        local
 
[root@docker1 centos_zabbix]# docker network create --driver macvlan --subnet 10.0.0.0/24 --gateway 10.0.0.254 -o parent=ens33 macvlan_1
Both of this commands are executed,Meaning addmacvlanNetwork type
[root@docker1 centos_zabbix]# docker network ls
NETWORK ID     NAME        DRIVER       SCOPE
19ac9a55bedb    bridge       bridge       local
0a3cbfe2473f    host        host        local
4e5ccdc657e1    macvlan_1      macvlan       local
aab77f02a0b1    none        null        local

Let both servers have a centos6.9:v2 version image:

Execute on one:
[root@docker1 centos_zabbix]# docker run -it --network macvlan_1 --ip=10.0.0.155 centos6.9:v2 /bin/bash
Execute on one:
[root@docker2 opt]# docker run -it --network macvlan_1 --ip=10.0.0.156 centos6.9_ssh:v2 /bin/bash
[root@9ea3f69d89c4 /]# ping 10.0.0.155
PING 10.0.0.155 (10.0.0.155) 56(84) bytes of data.
64 bytes from 10.0.0.155: icmp_seq=1 ttl=64 time=2.15 ms
64 bytes from 10.0.0.155: icmp_seq=2 ttl=64 time=0.569 ms CanpingThrough another container, the containers are the same.
^C

This problem is

The IP address cannot be repeated. If there is duplication, it will cause confusion. For example, both IPs with 155 will appear, and the ping155 network can be pinged, but SSH does not know which one is connected. In fact, if you can't connect, the link will be errored, so the overlay network is introduced to manage IP.

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.