I ran across the above issue today and found this blog post that helped me out. Essentially you create a new network device on your host, assign it an IP, bring it up, and modify the routing tables so that traffic to the macvlan subnet go via that IP.
My setup is slightly different because unlike the author I don’t randomly assign IPs in my macvlan network. As I alluded to in an earlier post I create a macvlan network and then assign an IP to each container in that network. I’ll repeat the same below just to recap.
Create the macvlan network:
1 2 3 4 5 |
docker network create -d macvlan \ --subnet="$SUBNET" \ --gateway="$GATEWAY" \ -o parent=eth0 \ $NETWORK |
Now create a container and assign it an IP manually:
1 2 3 4 5 |
docker create --name "$NAME" \ -P --network="$NETWORK" --ip=$IP \ --restart=unless-stopped \ --cap-add=NET_ADMIN \ "$IMAGE" |
In my specific case the instructions from the blog post I linked to will be as below:
1 2 3 4 5 6 7 8 9 10 11 12 |
# create the interface; I call it macvlan0 instead of mynet-shim ip link add macvlan0 link eth0 type macvlan mode bridge # assign it an IP address; note the /32 subnet ip addr add 192.168.1.223/32 dev macvlan0 # bring it up ip link set macvlan0 up # tell my host to route to the container IP via this interface # 192.168.17.6 is the $IP assigned to the container above ip route add 192.168.1.20/32 dev macvlan0 |
It is a bit of a chore I know, my decision to assign IPs manually means I’ll have to repeat that last line for each new container in this macvlan network. But that’s fine by me.
Of course the above steps have to be redone upon a reboot. So I added them to my /etc/network/interfaces
file to automate it:
1 2 3 4 5 6 7 |
# macvlan for docker 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.1.233/32 dev macvlan0 up ip link set macvlan0 up post-up ip route add 192.168.1.20/32 dev macvlan0 |
Same commands as earlier, just that I create a “manual” interface and specified these commands via bunch of “pre-up” and “up” and “post-up” commands.
Hope this helps anyone else in the same situation!