Skip to content

MACVLAN for container networking: pros, cons, and a working setup

How MACVLAN gives each container its own MAC and IP on the host LAN, the four modes (bridge/private/vepa/passthru), and the operational gotchas around hosts not talking to their own MACVLAN children.

MACVLAN gives each container its own MAC and IP on the same L2 segment as the host. Instead of NAT or a host bridge with veth pairs, the container’s interface looks exactly like a physical NIC plugged into the same switch port. That makes services discoverable by neighbour broadcasts, simplifies firewall reasoning, and removes a hop of latency. The cost is the well-known restriction that the host cannot talk to its own MACVLAN children, plus the fact that wireless and some cloud underlays refuse to learn additional MACs. This article walks through the four modes, the working bridge-mode setup, and the host-loopback workaround.

How to verify

Inspect MACVLAN interfaces and modes:

ip -d link show
ip link show type macvlan
bridge link show
arp -an | grep <container-mac>
tcpdump -ni eth0 ether host <container-mac>
ip neigh show dev eth0

The ip -d link show output names the macvlan mode explicitly (e.g., macvlan mode bridge); confirm yours matches the intended mode before debugging further.

What’s happening

MACVLAN is a kernel feature that creates a virtual NIC tied to a parent (typically eth0) but with its own MAC address. Frames are demultiplexed by destination MAC: a frame addressed to the MACVLAN child is delivered to that child, not to the parent’s stack. Conversely, frames the child sends use the child’s MAC as source and leave via the parent NIC unchanged.

The mode controls how children see each other and the host:

  • bridge (most common): children can communicate with each other directly through the kernel without the frame leaving the host. They cannot talk to the parent’s stack — that is the host-comm restriction.
  • private: children are isolated from each other. They can only reach hosts off-box.
  • vepa (Virtual Ethernet Port Aggregator): all child-to-child traffic is forced out the parent and back in, so a 802.1Qbg-aware switch can apply policy. Requires switch support.
  • passthru: only one child is allowed per parent and it consumes the parent entirely; mostly useful for SR-IOV and PCI passthrough scenarios.

The host-to-child restriction is the most surprising property. Frames from the host’s stack out eth0 carry the host’s MAC; the kernel does not loop these back to MACVLAN children, so ping <child-ip> from the host hangs even though ping <child-ip> from another machine on the same VLAN works fine. The workaround is to create a MACVLAN child on the host’s own namespace that the host uses for talking to its children.

The procedure

  1. Create a MACVLAN child for a container. Assume host parent is eth0 on 192.0.2.0/24 with gateway 192.0.2.1. The container will get 192.0.2.50:

    ip link add mvlan-app1 link eth0 type macvlan mode bridge
    ip link set mvlan-app1 netns app1
    ip -n app1 addr add 192.0.2.50/24 dev mvlan-app1
    ip -n app1 link set mvlan-app1 up
    ip -n app1 route add default via 192.0.2.1
  2. For Docker, use the macvlan network driver. It wires this up natively:

    docker network create -d macvlan \
      --subnet=192.0.2.0/24 \
      --gateway=192.0.2.1 \
      -o parent=eth0 \
      pub_net
    docker run -d --network pub_net --ip=192.0.2.50 nginx
  3. Add the host-loopback shim if the host needs to reach its own MACVLAN children:

    ip link add mvlan-host link eth0 type macvlan mode bridge
    ip addr add 192.0.2.2/32 dev mvlan-host
    ip link set mvlan-host up
    ip route add 192.0.2.50/32 dev mvlan-host

    The host now uses mvlan-host as a sibling MACVLAN endpoint to reach 192.0.2.50 — both children, no host-comm restriction.

  4. Disable promiscuous mode unless required. Bridges and many CNIs enable promisc on the parent; for MACVLAN you do not need it, and leaving promisc on costs CPU on busy NICs.

    ip link set eth0 promisc off
  5. Check the switch port. Most enterprise switches accept multiple MACs per port by default, but some access ports enforce port-security with a low MAC limit. Verify with show port-security on the switch (or its equivalent) before deploying many MACVLAN children.

  6. Persist with systemd-networkd.

    # /etc/systemd/network/20-mvlan-app1.netdev
    [NetDev]
    Name=mvlan-app1
    Kind=macvlan
    
    [MACVLAN]
    Mode=bridge
    # /etc/systemd/network/20-mvlan-app1.network
    [Match]
    Name=mvlan-app1
    
    [Network]
    Address=192.0.2.50/24
    Gateway=192.0.2.1

Operational notes

  • The host cannot ping its own children. This is by design — bridge mode does not loop traffic between parent and child. Either add the shim above, or use IPVLAN L3 mode where this restriction does not apply.
  • Wi-Fi rejects multiple MACs. Most wireless drivers and APs refuse to relay frames from MACs other than the one associated to the AP. MACVLAN is effectively wired-only.
  • Cloud underlays may filter unknown MACs. AWS VPC, Azure VNet, and Hetzner Cloud all drop frames whose source MAC differs from the registered NIC MAC. IPVLAN (which shares the parent MAC) is the cloud-friendly variant.
  • DHCP collisions. Two MACVLAN children claiming addresses by DHCP on the same broadcast domain need distinct client identifiers; the default dhclient uses the MAC, which is fine, but systemd-networkd’s default DUID derivation can collide.
  • MTU is inherited from the parent. Setting MTU on the child only adjusts the child’s view; the parent must agree. For jumbo frames, set MTU on eth0 first.

In the engagements we run, MACVLAN is the right answer when a containerised service needs to look like a real host on the office LAN — typical for legacy DCOM/DCE-RPC software, certain monitoring probes, and any service that gets discovered by mDNS or NetBIOS. For dense container fabrics the VXLAN overlay pattern wins because it does not stress the underlay’s MAC table; both deployment patterns are documented in the managed-operations playbook.