Docker Networking On Detail - Examples
Docker Networking On Detail - Examples
When to use it: Avoid using the Default Bridge, use another network type instead
where possible
The host network mode Linux only and is not supported on Windows or Mac.
When to use it: Handy when a container needs to expose a large number of ports,
or the container runs an application that you would like to act as if running natively.
Useful for things like VPN servers for example
When to use it: When you don’t want a container to be connected to a network at
all. Useful for complete network isolation for containers that don’t need network
access. Containers exclusively operating on files on a volume such as producing
backups, artifacts, or auditing for example.
Better isolation
All containers without the --network flag defined are by default attached the the
default bridge network, which can be risky as unrelated stacks, services and
containers are able to communicate.
You can create a new user-defined bridge network by running docker network
create <network-name>.
When to use it: Most of the time, you will want to create your containers in their
own user-defined networks. Running a stack of applications in a user-defined
network for that stack enables each container in the stack to talk to each other, but
without exposing the containers to anything more than necessary. A stack
containing a web and db container might expose port 80 to the web, but keep the
database completely isolated.
When to use it: When you have a large number of docker hosts in a swarm, and
want them to be able to communicate with eachother at a greater level of
abstraction.
6. The MACVLAN
Bridge Mode
MACVLAN networks are bound directly to the physical port on the host, and
configured to communicate directly to the physical networks. Containers have
their own MAC addresses and IP addresses, similar to as though they were virtual
machines. Some networks will not allow multiple MAC addresses to be available
on one Switch port, and so Promiscuous mode must be enabled on each device
between the host and the router.
801.1q Mode
Works just like the Bridge Mode, but can also be configured with VLANs as sub-
interfaces.
When to use it: When you want your containers to also be VLAN aware
7. The IPVLAN
Works similarly to MACVLAN but with the difference of all containers within using
the same MAC address as the host interface. This means we don’t need to fight
with Promiscuous mode. Works similarly to the HOST mode, but can specify a
second host interface. IPVLAN also support VLAN sub-interfaces.
When to use it: When you want your containers to act as though they’re on the
same network as your router, but do not require their own MAC addresses.
IPVLAN L3 Mode
Adds Layer 3 routing to the network mode. This means no switches, no ARP, and no
broadcast/multicast traffic coming out of the host. The host then acts as a router.
This can be created by running: docker network create -d ipvlan --subnet
10.1.2.0/24 --gateway 10.1.2.1 -o ipvlan_mode=l3 <network-name>
When to use it: When you the host to act as a router for containers in the network,
stopping broadcast traffic from containers from reaching the network the host is
attached to.
Macvlan and Ipvlan Network Drivers
Getting Started
The Macvlan and Ipvlan drivers are currently in experimental mode in order to
incubate Docker users use cases and vet the implementation to ensure a
hardened, production ready driver in a future release. Libnetwork now gives
users total control over both IPv4 and IPv6 addressing. The VLAN drivers build
on top of that in giving operators complete control of layer 2 VLAN tagging and
even Ipvlan L3 routing for users interested in underlay network integration. For
overlay deployments that abstract away physical constraints see the multi-host
overlay driver.
Macvlan and Ipvlan are a new twist on the tried and true network virtualization
technique. The Linux implementations are extremely lightweight because rather
than using the traditional Linux bridge for isolation, they are simply associated
to a Linux Ethernet interface or sub-interface to enforce separation between
networks and connectivity to the physical network.
Macvlan and Ipvlan offer a number of unique features and plenty of room for
further innovations with the various modes. Two high level advantages of these
approaches are, the positive performance implications of bypassing the Linux
bridge and the simplicity of having less moving parts. Removing the bridge that
traditionally resides in between the Docker host NIC and container interface
leaves a very simple setup consisting of container interfaces, attached directly
to the Docker host interface. This result is easy access for external facing
services as there is no port mappings in these scenarios.
Pre-Requisites
All of the examples can be performed on a single host running Docker.
Any examples using a sub-interface like eth0.10 can be replaced with
eth0 or any other valid parent interface on the Docker host. Sub-
interfaces with a . are created on the fly. -o parent interfaces can also
be left out of the docker network create all together and the driver
will create a dummy interface that will enable local host connectivity to
perform the examples.
Kernel requirements:
To check your current kernel version, use uname -r to display your
kernel version
Macvlan Linux kernel v3.9–3.19 and 4.0+
Ipvlan Linux kernel v4.2+ (support for earlier kernels exists but is buggy)
MacVlan Bridge Mode Example Usage
Macvlan Bridge mode has a unique MAC address per container used to track
MAC to port mappings by the Docker host. This is the largest difference from
Ipvlan L2 mode which uses the same MAC address as the parent interface for
each container eth0 interface.
Macvlan and Ipvlan driver networks are attached to a parent Docker host
interface. Examples are a physical interface such as eth0, a sub-
interface for 802.1q VLAN tagging like eth0.10 (.10 representing VLAN
10) or even bonded host adaptors which bundle two Ethernet interfaces
into a single logical interface.
The specified gateway is external to the host provided by the network
infrastructure.
Each Macvlan Bridge mode Docker network is isolated from one another
and there can be only one network attached to a parent interface at a
time. There is a theoretical limit of 4,094 sub-interfaces per host adaptor
that a Docker network could be attached to.
It is not recommended to mix ipvlan and macvlan networks on the same -
o parent= interface. Older kernel versions will throw uninformative
netlink errors such as device is busy.
Any container inside the same subnet can talk any other container in the
same network without a gateway in both macvlan bridge mode and
ipvlan L2 modes.
The same docker network commands apply to the vlan drivers. Some
are irrelevant such as -icc or --set-macaddress for the Ipvlan driver.
In Macvlan and Ipvlan L2 mode, containers on separate networks cannot
reach one another without an external process routing between the two
networks/subnets. This also applies to multiple subnets within the same
docker network. See Ipvlan L3 mode for inter-subnet communications
without a router.
In the following example, eth0 on the docker host has an IP on the
172.16.86.0/24 network and a default gateway of 172.16.86.1. The
gateway is an external router with an address of 172.16.86.1. An IP address
is not required on the Docker host interface eth0 in bridge mode, it merely
needs to be on the proper upstream network to get forwarded by a network
switch or network router.
Note For Macvlan bridge mode and Ipvlan L2 mode the subnet values need to
match the NIC's interface of the Docker host. For example, Use the same
subnet and gateway of the Docker host ethernet interface that is specified by
the -o parent= option.
The parent interface used in this example is eth0 and it is on the subnet
172.16.86.0/24. The containers in the docker network will also
need to be on this same subnet as the parent -o parent=. The gateway
is an external router on the network, not any ip masquerading or any
other local proxy.
The driver is specified with -d driver_name option. In this case -d
macvlan
Create the macvlan network and run a couple of containers attached to it:
ip route
default via 172.16.86.1 dev eth0
172.16.86.0/24 dev eth0 src 172.16.86.2
While the eth0 interface does not need to have an IP address in Macvlan
Bridge mode or Ipvlan L2 mode it is not uncommon to have an IP address on
the interface. Addresses can be excluded from getting an address from the
default built in IPAM by using the --aux-address=x.x.x.x flag. This will
blacklist the specified address from being handed out to containers. The same
network example above blocking the -o parent=eth0 address from being
handed out to a container.
Note: In both Macvlan and Ipvlan you are not able to ping or
communicate with the default namespace IP address. For example, if you
create a container and try to ping the Docker host's eth0 it will not work.
That traffic is explicitly filtered by the kernel modules themselves to offer
additional provider isolation and security.
For more on Docker networking commands see Working with Docker network
commands
Ipvlan L2 Mode Example Usage
The ipvlan L2 mode example is virtually identical to the macvlan bridge mode
example. The driver is specified with -d driver_name option. In this case -d
ipvlan
The parent interface in the next example -o parent=eth0 is configured as
followed:
Use the network from the host's interface as the --subnet in the docker
network create. The container will be attached to the same network as the
host interface as set via the -o parent= option.
Create the ipvlan network and run a container attaching to it:
The default mode for Ipvlan is l2. The default mode for Macvlan is bridge. If -
o ipvlan_mode= or -o macvlan_mode= are left unspecified, the default
modes will be used. Similarly, if the --gateway is left empty, the first usable
address on the network will be set as the gateway. For example, if the subnet
provided in the network create is --subnet=192.168.1.0/24 then the
gateway the container receives is 192.168.1.1.
To help understand how this mode interacts with other hosts, the following
figure shows the same layer 2 segment between two Docker hosts that applies
to both Macvlan Bride mode and Ipvlan L2 mode.
The following will create the exact same network as the network db_net
created prior, with the driver defaults for --gateway=192.168.1.1 and -o
ipvlan_mode=l2.
The drivers also support the --internal flag that will completely isolate
containers on a network from any communications external to that network.
Since network isolation is tightly coupled to the network's parent interface the
result of leaving the -o parent= option off of a network create is the exact
same as the --internal option. If the parent interface is not specified or the
--internal flag is used, a netlink type dummy parent interface is created for
the user and used as the parent interface effectively isolating the network
completely.
The following two docker network create examples result in identical
networks that you can attach container to:
Replace the macvlan with ipvlan in the -d driver argument to create macvlan
802.1q trunks.
Vlan ID 50
In the first network tagged and isolated by the Docker host, eth0.50 is the
parent interface tagged with vlan id 50 specified with -o parent=eth0.50.
Other naming formats can be used, but the links need to be added and deleted
manually using ip link or Linux configuration files. As long as the -o parent
exists anything can be used if compliant with Linux netlink.
Vlan ID 60
In the second network, tagged and isolated by the Docker host, eth0.60 is the
parent interface tagged with vlan id 60 specified with -o parent=eth0.60.
The macvlan_mode= defaults to macvlan_mode=bridge. It can also be
explicitly set with the same result as shown in the next example.
The option to use either existing parent vlan sub-interfaces or let Docker
manage them enables the user to either completely manage the Linux
interfaces and networking or let Docker create and delete the Vlan parent sub-
interfaces (netlink ip link) with no effort from the user.
For example: eth0.10 to denote a sub-interface of eth0 tagged with vlan id
10. The equivalent ip link command would be ip link add link eth0
name eth0.10 type vlan id 10.
The example creates the vlan tagged networks and then start two containers to
test connectivity between containers. Different Vlans cannot ping one another
without a router routing between the two networks. The default namespace is
not reachable per ipvlan design in order to isolate container namespaces from
the underlying host.
Vlan ID 20
In the first network tagged and isolated by the Docker host, eth0.20 is the
parent interface tagged with vlan id 20 specified with -o parent=eth0.20.
Other naming formats can be used, but the links need to be added and deleted
manually using ip link or Linux configuration files. As long as the -o parent
exists anything can be used if compliant with Linux netlink.
Vlan ID 30
In the second network, tagged and isolated by the Docker host, eth0.30 is the
parent interface tagged with vlan id 30 specified with -o parent=eth0.30.
The ipvlan_mode= defaults to l2 mode ipvlan_mode=l2. It can also be
explicitly set with the same result as shown in the next example.
The gateway is set inside of the container as the default gateway. That gateway
would typically be an external router on the network.
$ ip route
default via 192.168.30.1 dev eth0
192.168.30.0/24 dev eth0 src 192.168.30.2
A key takeaway is, operators have the ability to map their physical network into
their virtual network for integrating containers into their environment with no
operational overhauls required. NetOps simply drops an 802.1q trunk into the
Docker host. That virtual link would be the -o parent= passed in the network
creation. For untagged (non-VLAN) links, it is as simple as -o parent=eth0 or
for 802.1q trunks with VLAN IDs each network gets mapped to the
corresponding VLAN/Subnet from the network.
An example being, NetOps provides VLAN ID and the associated subnets for
VLANs being passed on the Ethernet link to the Docker host server. Those
values are simply plugged into the docker network create commands
when provisioning the Docker networks. These are persistent configurations
that are applied every time the Docker engine starts which alleviates having to
manage often complex configuration files. The network interfaces can also be
managed manually by being pre-created and docker networking will never
modify them, simply use them as parent interfaces. Example mappings from
NetOps to Docker network commands are as follows:
VLAN: 10, Subnet: 172.16.80.0/24, Gateway: 172.16.80.1
--subnet=172.16.80.0/24 --gateway=172.16.80.1 -o
parent=eth0.10
Ipvlan L3 mode drops all broadcast and multicast traffic. This reason alone
makes Ipvlan L3 mode a prime candidate for those looking for massive scale
and predictable network integrations. It is predictable and in turn will lead to
greater uptimes because there is no bridging involved. Bridging loops have
been responsible for high profile outages that can be hard to pinpoint
depending on the size of the failure domain. This is due to the cascading nature
of BPDUs (Bridge Port Data Units) that are flooded throughout a broadcast
domain (VLAN) to find and block topology loops. Eliminating bridging domains,
or at the least, keeping them isolated to a pair of ToRs (top of rack switches)
will reduce hard to troubleshoot bridging instabilities. Macvlan Bridge and
Ipvlan L2 modes are well suited for isolated VLANs only trunked into a pair of
ToRs that can provide a loop-free non-blocking fabric. The next step further is
to route at the edge via Ipvlan L3 mode that reduces a failure domain to a local
host only.
L3 mode needs to be on a separate subnet as the default namespace
since it requires a netlink route in the default namespace pointing to the
Ipvlan parent interface.
The parent interface used in this example is eth0 and it is on the subnet
192.168.1.0/24. Notice the docker network is not on the same
subnet as eth0.
Unlike macvlan bridge mode and ipvlan l2 modes, different
subnets/networks can ping one another as long as they share the same
parent interface -o parent=.
ip a show eth0
3: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500
qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:50:56:39:45:2e brd ff:ff:ff:ff:ff:ff
inet 192.168.1.250/24 brd 192.168.1.255 scope
global eth0
# Create a v6 network
docker network create -d ipvlan \
--subnet=2001:db8:abc2::/64 --
gateway=2001:db8:abc2::22 \
-o parent=eth0.139 v6ipvlan139
root@5c1dc74b1daa:/# ip -6 route
2001:db8:abc4::/64 dev eth0 proto kernel metric 256
2001:db8:abc2::/64 dev eth0 proto kernel metric 256
default via 2001:db8:abc2::22 dev eth0 metric 1024
The next example with setup a dual stack IPv4/IPv6 network with an example
VLAN ID of 140.
Next create a network with two IPv4 subnets and one IPv6 subnets, all of which
have explicit gateways:
Start a container and view eth0 and both v4 & v6 routing tables:
root@3cce0d3575f3:/# ip route
default via 192.168.140.1 dev eth0
192.168.140.0/24 dev eth0 proto kernel scope link
src 192.168.140.2
root@3cce0d3575f3:/# ip -6 route
2001:db8:abc4::/64 dev eth0 proto kernel metric 256
2001:db8:abc9::/64 dev eth0 proto kernel metric 256
default via 2001:db8:abc9::22 dev eth0 metric 1024
Start a second container with a specific --ip4 address and ping the first host
using ipv4 packets:
root@3a368b2a982e:/# ip -6 route
2001:db8:abc4::/64 dev eth0 proto kernel metric 256
2001:db8:abc6::/64 dev eth0 proto kernel metric 256
default dev eth0 metric 1024
Note: There may be a bug when specifying --ip6= addresses when you delete
a container with a specified v6 address and then start a new container with the
same v6 address it throws the following like the address isn't properly being
released to the v6 pool. It will fail to unmount the container and be left dead.
Links if manually created can be named anything you want. As long as the exist
when the network is created that is all that matters. Manually created links do
not get deleted regardless of the name when the network is deleted with
docker network rm.
As with all of the Libnetwork drivers, they can be mixed and matched, even as
far as running 3rd party ecosystem drivers in parallel for maximum flexibility to
the Docker user.