I won’t go into the what of macvlans as there are already plenty of articles which cover the topic, including this one directly from Docker. However you may soon find yourself in a situation where your container needs to access a service on the host but it can’t access it. In order to make this work, you need to assign an ip address from the macvlan on the host. This can be easily accomplished in a few commands:
ip link add macvlan0 link eth0 type macvlan mode bridge
ip addr add 192.168.100.31/32 dev macvlan0
ip link set macvlan0 up
ip route add 192.168.100.24/29 dev macvlan0
However after a reboot you might be disappointed to find that this no longer works. Creating an interface is distribution dependent so this only applies to Debian. Create a new file named /etc/network/interfaces.d/macvlan0.
sudo vi /etc/network/interfaces.d/macvlan0
Then add the following commands to the file:
auto macvlan0
iface macvlan0 inet manual
pre-up ip link add macvlan0 link eth0 type macvlan mode bridge
pre-up ip addr add 192.168.100.31/32 dev macvlan0
up ip link set macvlan0 up
post-up ip route add 192.168.100.24/29 dev macvlan0
You should add in pre-down/down/post-down commands to the interfaces.d file to clean up the route/interface, otherwise a “systemctl restart networking” will error out when it tries to re-create the existing interface.
Thanks, exactly what I was looking for for my Raspberry Pis. Do you also know how to do this for an Ubuntu using netplan?