Using macvlan to create docker container with ip in host network subnet
On 2018/05/28 at 23:00
In previous post Create VM with ip bridge to host network (in the same network as host), I create a bridge and tap device to bridge VM network with host network. However in recent kernel update, macvlan is supported to have ethernet device virtualization. And docker has built-in support for macvlan feature.
So, I can launch containers in the same host network subnet easily and quickly.
Instructions
Create macvlan docker network. The subnet and gateway should be the same as my host network and the parent
option should assing to the host ethernet interface.
$ docker network create -d macvlan --subnet=10.1.14.0/24 --gateway=10.1.14.1 -o parent=enp5s2 macvlan
Launch containers with macvlan network.
> $ docker run -ti --network macvlan myubuntu ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
19: [email protected]: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:0a:01:0e:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 10.1.14.2/24 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::42:aff:fe01:e02/64 scope link tentative
valid_lft forever preferred_lft forever