Sub topic 14
Macvlan and ipvlan: containers on the physical network
This chapter assumes you know how bridge networks, published ports, and Docker DNS work, and that you understand IP subnets and gateways. Bridge networks hide containers behind NAT on the host. Macvlan and ipvlan do the opposite. They give each container an address on the physical network, so other machines on the LAN can reach it directly without published ports.
When to use these drivers
Use macvlan or ipvlan when a container must look like a normal machine on your LAN. Typical cases are legacy software that expects to sit on a physical network, services that need a fixed LAN address, and tools that cannot work through NAT. For most applications, a user-defined bridge with published ports is simpler and safer.
How macvlan works
The macvlan driver creates a virtual network interface for each container. Each interface has its own MAC address and is attached to a parent interface on the host, such as eth0. The LAN switch sees several MAC addresses on one port, and each container answers for its own IP.
docker network create -d macvlan \
--subnet=192.168.1.0/24 \
--gateway=192.168.1.1 \
-o parent=eth0 \
lan-netAdjust the subnet, gateway, and parent interface to match your environment. The subnet and gateway must be the ones your physical network really uses.
Start a container on this network and give it a free address:
docker run -d --name web-lan --network lan-net --ip 192.168.1.50 nginx:alpineOther machines on the LAN can now open http://192.168.1.50 directly. No -p flag is needed.
Gotchas with macvlan
- The host cannot reach its own macvlan containers. Linux blocks traffic between a parent interface and its macvlan children. Test from another machine, or create a macvlan sub-interface on the host as a workaround.
- Address conflicts. Docker does not talk to your DHCP server. If the container range overlaps addresses your router hands out, two machines get the same IP. Reserve a range, and limit Docker to it with
--ip-range. - Promiscuous mode. The parent interface often needs promiscuous mode. Virtual machines and some cloud networks reject extra MAC addresses, so macvlan may not work there at all.
- Switch limits. Some switches restrict the number of MAC addresses per port.
- No Docker DNS isolation from the LAN. Containers are exposed to the whole LAN, so you lose the protection that NAT and unpublished ports give you. Apply firewall rules on the network.
ipvlan
The ipvlan driver is similar, but all containers share the parent interface's MAC address and differ only by IP. This avoids the MAC-address limits and promiscuous-mode needs described above, so it works in more environments.
docker network create -d ipvlan \
--subnet=192.168.1.0/24 \
--gateway=192.168.1.1 \
-o parent=eth0 \
-o ipvlan_mode=l2 \
lan-ipvlanipvlan has two main modes:
| Mode | Behavior |
|---|---|
| `l2` | Containers share the LAN's layer 2 segment, like macvlan but with one MAC. |
| `l3` | The host routes traffic between containers and the LAN. Broadcast and multicast do not pass, and the LAN needs routes to the container subnets. |
Choosing a driver
| Need | Choice |
|---|---|
| Containers talking on one host | User-defined bridge |
| Distinct MAC per container, physical LAN | macvlan |
| Many addresses, restricted MAC use, or VMs | ipvlan |
| Containers across hosts with encryption options | Overlay |
Exercise
A colleague creates a macvlan network on eth0 and starts a container at 192.168.1.50. Other laptops reach it, but curl 192.168.1.50 from the Docker host times out. Why does this happen, and what are two ways to deal with it?
Show answer
Linux does not let a parent interface exchange traffic with its own macvlan children. Either test from another machine, or create a macvlan sub-interface on the host with an address in the same subnet and route container traffic through it. Using an ipvlan l2 network avoids the separate-MAC issue but still has the same parent-to-child restriction, so the host sub-interface approach is the usual fix.