Networking macvlan

When setting up macvlan for docker meaning each container can get an external IP on our local network. Also need to make sure those containers can communicate with each other

Temporary

We want to achieve the following in macvlan, however this is only a temporary solution as config will disappear after a reboot.

1
2
3
4
ip link add macvlan link eth0 type macvlan mode bridge
ip addr add ${GATEWAY_IP} dev macvlan
ip link set macvlan up
ip route add ${MACVLAN1_IP |- 172.16.31.11} dev macvlan

Permanent

Need to create a file which on startup will create a macvlan bridge

/etc/networkd-dispatcher/routable.d/10-macvlan-interfaces.sh

1
2
3
#! /bin/bash

ip link add macvlan link eth0 type macvlan mode bridge

Now we got an interface, will need to create mapping for the docker containers we want to map

/etc/netplan/macvlan.yaml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
network:
    version: 2
    renderer: networkd
    ethernets:
        macvlan0:
            addresses:
                - 172.16.31.5/32
            routes:
                - to: ${MACVLAN1_IP |- 172.16.31.11}
                  via: ${GATEWAY_IP}
                  metric: 100

Docker

Now we can create a macvlan network

1
2
3
4
5
6
docker network create -d macvlan \
  --subnet=172.16.31.0/24 \
  --gateway=172.16.31.1 \
  --ip-range=172.16.31.48/28 \
  -o parent=eth0 \
  my_macvlan_net

example lunching a docker container

1
docker run -d --name test --network my_macvlan_net --ip 172.16.31.11 nginx
All rights reserved