Docker : Création d'un réseau macvlan en mode bridge

Distribution : Ubuntu 21.04 LTS

Pour en savoir plus : https://www.linuxtechi.com/create-use-macvlan-network-in-docker/

1/ Visualisation des réseaux disponibles

util01@station40:~$ docker network ls
NETWORK ID     NAME      DRIVER    SCOPE
87441430cd51   bridge    bridge    local
3bef216e602f   host      host      local
75d78fa8ec2e   none      null      local
util01@station40:~$

2/ Visualisation des interfaces réseaux de l'hôte.

util01@station40:~$ ifconfig
docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:a8:98:ce:e8  txqueuelen 0  (Ethernet)
        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

enp3s0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        ether 10:78:d2:95:4c:00  txqueuelen 1000  (Ethernet)
        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

enp4s1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.86  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::9fe:d00c:d75e:66b3  prefixlen 64  scopeid 0x20<link>
        ether c0:a0:bb:e1:fa:39  txqueuelen 1000  (Ethernet)
        RX packets 1204465  bytes 1682571240 (1.6 GB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 690584  bytes 63750696 (63.7 MB)
        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
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 6274  bytes 2444225 (2.4 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 6274  bytes 2444225 (2.4 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

util01@station40:~$ 

3/ Création d'un réseau en 'mode bridge'.

util01@station40:~$ docker network create -d macvlan --subnet=192.168.1.0/24 --gateway=192.168.1.1 -o parent=enp4s1 reseau01
93917b64545492e9ed897536fcb176d2acb8c9fb74f0ebf08eb7868ff666933b                                                                          
util01@station40:~$

4/ Visualisation des drivers réseaux.

util01@station40:~$ docker network ls                                                                                                     
NETWORK ID     NAME       DRIVER    SCOPE                                                                                                 
87441430cd51   bridge     bridge    local                                                                                                 
3bef216e602f   host       host      local                                                                                                 
75d78fa8ec2e   none       null      local                                                                                                 
93917b645454   reseau01   macvlan   local                                                                                                 
util01@station40:~$ 

5/ Création d'un conteneur.

util01@station40:~$ docker run --rm -itd --network=reseau01 --ip=192.168.1.110 alpine:latest /bin/sh                                      
Unable to find image 'alpine:latest' locally                                                                                              
latest: Pulling from library/alpine                                                                                                       
8921db27df28: Pull complete 
Digest: sha256:f271e74b17ced29b915d351685fd4644785c6d1559dd1f2d4189a5e851ef753a                                                           
Status: Downloaded newer image for alpine:latest                                                                                          
f38bcf334c01e2c98b4ba3a8f0635ada46988c72c2d91027a5184d2a1d1c0fd0                                                                          
util01@station40:~$

6/ Listage des conteneurs actifs.

util01@station40:~$ docker ps
CONTAINER ID   IMAGE           COMMAND     CREATED          STATUS          PORTS     NAMES                                               
f38bcf334c01   alpine:latest   "/bin/sh"   42 seconds ago   Up 38 seconds             angry_hamilton
util01@station40:~$ 

7/ Inspection d'un conteneur.

util01@station40:~$ docker container inspect f38bcf334c01
[
    {
        "Id": "f38bcf334c01e2c98b4ba3a8f0635ada46988c72c2d91027a5184d2a1d1c0fd0",
        "Created": "2023-01-22T12:53:28.800531805Z",
        "Path": "/bin/sh",
        "Args": [],
        "State": {
            "Status": "running",
...
            "Networks": {
                "reseau01": {
                    "IPAMConfig": {
                        "IPv4Address": "192.168.1.110"
                    },
                    "Links": null,
                    "Aliases": [
                        "f38bcf334c01"
                    ],
                    "NetworkID": "93917b64545492e9ed897536fcb176d2acb8c9fb74f0ebf08eb7868ff666933b",
                    "EndpointID": "a3bd56c31b3f091c6d5121a994e64b8ba93a15936ec1f57a31ee17cb0e68cb1c",
                    "Gateway": "192.168.1.1",
                    "IPAddress": "192.168.1.110",
                    "IPPrefixLen": 24,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:c0:a8:01:6e",
                    "DriverOpts": null
                }
            }
        }
    }
]
util01@station40:

8/ Création d'un 2ème conteneur.

util01@station40:~$ docker run --rm -itd --network=reseau01 alpine:latest /bin/sh
ae268aa13c7898757a05d0eb7cb119d41f67cb2af39bcbd96eedd3f67ea9aae1
util01@station40:~$ 

9/ Affichage des conteneurs actifs.

util01@station40:~$ docker ps
CONTAINER ID   IMAGE           COMMAND     CREATED          STATUS          PORTS     NAMES
ae268aa13c78   alpine:latest   "/bin/sh"   16 seconds ago   Up 14 seconds             beautiful_sutherland
f38bcf334c01   alpine:latest   "/bin/sh"   3 minutes ago    Up 3 minutes              angry_hamilton
util01@station40:~$ 

10/ Visualisation des adresses ipv4 des conteneurs.

util01@station40:~$ docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' ae268aa13c78
192.168.1.2
util01@station40:~$ docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' f38bcf334c01
192.168.1.110
util01@station40:~$ 

11/ Test de connexion entre les 2 conteneurs.

  • Connexion au conteneur 'ae268aa13c78' :
util01@station40:~$ docker exec -it ae268aa13c78 /bin/sh
/ # 
  • Ping vers le conteneur 'f38bcf334c01' :
/ # ping 192.168.1.110 -c 3
PING 192.168.1.110 (192.168.1.110): 56 data bytes
64 bytes from 192.168.1.110: seq=0 ttl=64 time=0.100 ms
64 bytes from 192.168.1.110: seq=1 ttl=64 time=0.102 ms
64 bytes from 192.168.1.110: seq=2 ttl=64 time=0.113 ms

--- 192.168.1.110 ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 0.100/0.105/0.113 ms
/ # 

12/ Création d'une interface 'macvlan' sur l'hôte.

util01@station40:~$ sudo ip link add interface01 link enp4s1 type macvlan mode bridge

13/ Assigner une adresse ip à l'interface réseau 'interface01'.

util01@station40:~$ sudo ip addr add 192.168.1.200/32 dev interface01

14/ Activer l'interface réseau.

util01@station40:~$ sudo ip link set interface01 up

15/ Ajouter une route au reseau 'macvlan'.

util01@station40:~$ sudo ip route add 192.168.1.0/24 dev interface01

16/ Test : Ping vers les 2 conteneurs.

util01@station40:~$ ping 192.168.1.2 -c 2
PING 192.168.1.2 (192.168.1.2) 56(84) bytes of data.
64 bytes from 192.168.1.2: icmp_seq=1 ttl=64 time=0.137 ms
64 bytes from 192.168.1.2: icmp_seq=2 ttl=64 time=0.080 ms

--- 192.168.1.2 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1007ms
rtt min/avg/max/mdev = 0.080/0.108/0.137/0.028 ms
util01@station40:~$ ping 192.168.1.110 -c 2
PING 192.168.1.110 (192.168.1.110) 56(84) bytes of data.
64 bytes from 192.168.1.110: icmp_seq=1 ttl=64 time=0.146 ms
64 bytes from 192.168.1.110: icmp_seq=2 ttl=64 time=0.072 ms

--- 192.168.1.110 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1031ms
rtt min/avg/max/mdev = 0.072/0.109/0.146/0.037 ms
util01@station40:~$ 

17/ Test : Ping d'un conteneur vers l'hôte.

  • Connexion au conteneur 'ae268aa13c78' :
util01@station40:~$ docker exec -it ae268aa13c78 /bin/sh
/ # 
  • Ping vers l'hôte :
util01@station40:~$ docker exec -it ae268aa13c78 /bin/sh
/ # ping 192.168.1.86 -c 2
PING 192.168.1.86 (192.168.1.86): 56 data bytes
64 bytes from 192.168.1.86: seq=0 ttl=64 time=0.221 ms
64 bytes from 192.168.1.86: seq=1 ttl=64 time=0.119 ms

--- 192.168.1.86 ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 0.119/0.170/0.221 ms
/ #