Sub topic 2
Network drivers at a glance
This chapter assumes you can run containers with docker run, use a shell, and read basic TCP/IP terms such as IP address, subnet, port, and DNS. It gives you a map of Docker's built-in network drivers so you can pick one before later chapters go into detail.
What a driver does
A network driver is the plug-in that decides how a Docker network is built and how its containers connect to each other and to the outside. Every network you create has exactly one driver. You choose it with the --driver (or -d) flag of docker network create. A container can join several networks, and each network can use a different driver.
You can see the drivers that are active on your host in the output of docker network ls.
docker network lsOn a fresh install you will see three networks named bridge, host, and none. Each one uses the driver of the same name.
The built-in drivers
| Driver | Where containers can talk | Typical use |
|---|---|---|
| `bridge` | Containers on the same host and the same bridge network | Default choice for single-host applications |
| `host` | The container shares the host's network stack | Maximum network performance, or services that need many ports |
| `none` | Nowhere. Only a loopback interface exists | Fully isolated batch jobs or custom networking |
| `overlay` | Containers on different Docker hosts | Multi-host setups with Swarm |
| `macvlan` | Containers appear as separate devices on the physical LAN | Legacy software that expects to be on the LAN |
| `ipvlan` | Like macvlan, but containers share the host's MAC address | LANs that limit the number of MAC addresses per port |
bridge
The bridge driver creates a software switch inside the host. Containers connect to it through virtual interfaces and get private IP addresses. Docker creates one bridge network named bridge automatically. You can also create your own bridge networks, called user-defined bridge networks. These add name-based discovery and better isolation, and they are the usual choice for real applications. The next chapters cover both kinds in detail.
host
With the host driver, the container does not get its own network namespace. It uses the host's interfaces and ports directly. There is no port publishing, because a service listening on port 80 in the container is listening on port 80 on the host. This removes the overhead of address translation. It also removes isolation, and two containers cannot listen on the same port.
none
The none driver gives the container only a loopback interface. Nothing can reach it and it can reach nothing. Use it for workloads that must not touch the network.
overlay
The overlay driver builds a virtual network that spans several Docker hosts. Traffic between hosts is encapsulated and carried over the hosts' own network. It requires swarm mode on the hosts.
macvlan and ipvlan
These drivers attach containers directly to a physical network interface. Containers get addresses from your LAN subnet. They are useful when something outside Docker must reach a container by its own IP address, with no port publishing.
Choosing a driver
The diagram shows a simple decision path. Start with the bridge driver unless a specific requirement points elsewhere.
Trying a driver
You select the driver when you create a network. This example creates a bridge network explicitly. Because bridge is the default driver, leaving out -d bridge gives the same result.
docker network create -d bridge demo-net
docker network ls --filter driver=bridge
docker network rm demo-netFor the host and none drivers you do not create a network. You attach a container to the existing one by name.
docker run --rm --network none busybox ip addrThe output lists only the lo interface, which confirms the container has no other connectivity.
Common mistakes
- Using the default
bridgenetwork for multi-container apps. It lacks name-based discovery, so containers must use IP addresses. - Choosing
hostfor convenience and then hitting port conflicts. - Expecting
overlayto work without initializing swarm mode first. - Using
macvlanon a Wi-Fi interface, which usually rejects extra MAC addresses.
Exercise
A team runs a web container and a database container on one host. The database must not be reachable from outside, and the web container should find it by name. Which driver do you choose, and why?
Show answer
A user-defined bridge network. It keeps traffic on a private software switch on the host and provides name-based discovery through Docker's embedded DNS. The default bridge network has no name resolution, and host, macvlan, or ipvlan would expose the database more widely than needed.
Exercise
Why can't you publish a port with -p on a container that uses the host driver, and what does that mean for port conflicts?
Show answer
The container shares the host's network stack, so its services already listen on the host's ports and there is nothing to publish. Two containers, or a container and a host process, cannot listen on the same port.