Sub topic 3
The default bridge network and its limits
This chapter assumes you can run containers with docker run, read basic TCP/IP (IP addresses, subnets, ports), and have seen the driver list from the previous chapter. It covers what happens when you start a container without any network options.
What the default bridge is
Docker creates a network named bridge on every host. It uses the bridge driver. On Linux, the host side of this network is a virtual switch called docker0. A container started without --network joins it automatically.
Each container gets a virtual Ethernet pair. One end sits inside the container's network namespace as eth0. The other end plugs into docker0. The default subnet is usually 172.17.0.0/16, and docker0 holds the gateway address, typically 172.17.0.1.
Seeing it in practice
Start two containers and read their addresses:
docker run -d --name web1 nginx:alpine
docker run -d --name web2 nginx:alpine
docker inspect web1 --format '{{.NetworkSettings.IPAddress}}'
docker inspect web2 --format '{{.NetworkSettings.IPAddress}}'Typical output is 172.17.0.2 and 172.17.0.3. Your addresses may differ. Docker hands them out in start order, so they can change after a restart.
Connectivity by IP works
Containers on the same bridge can reach each other by IP address. Use the address you got above:
docker run --rm busybox wget -qO- http://172.17.0.2The command prints the nginx welcome page. Containers also reach the internet. Docker rewrites their outbound traffic to use the host's address (NAT, covered in a later chapter).
The limits
Compare the IP request with a request by name:
docker run --rm busybox ping -c 1 web1This fails with bad address 'web1'. The default bridge has no automatic name resolution. Containers get a copy of the host's DNS settings, and nothing maps container names to addresses.
| Limit | Effect |
|---|---|
| No name-based discovery | You must use IP addresses, which change between runs. |
| All containers share one network | Any container on the bridge can reach any other. You cannot segment an application. |
| Shared by every unrelated workload | Containers you started for other projects are on the same network. |
| Settings apply to all containers | Changing options such as the subnet means reconfiguring the Docker daemon and restarting it. |
| Legacy linking only | `--link` can inject names, but it is deprecated. |
The link between containers and the outside is also one-way by default. Nothing outside the host can reach a container unless you publish a port with -p. That is covered in its own chapter.
Attaching and detaching
You can add or remove a running container from a network without restarting it:
docker network disconnect bridge web2
docker network connect bridge web2After you reconnect, web2 may receive a different IP address. This is another reason not to hard-code addresses.
Common errors
bad addressorName or service not knownwhen using a container name. You are on the default bridge. Move the containers to a user-defined bridge, which is the next topic.- Address changed after restart. The bridge assigns addresses dynamically. Do not store them in configuration.
- Subnet clash with a VPN or corporate network.
172.17.0.0/16can overlap with real networks you use. Traffic to those addresses then goes to the bridge instead. The fix is to change the default address pool in the daemon configuration.
Exercise
Two containers, api and db, were started with plain docker run and no --network option. api can ping the IP address of db but fails when it pings the name db. Why, and what does that tell you about the network they are on?
Show answer
Both containers are on the default bridge network. It connects containers at the IP level but provides no DNS resolution of container names, so only the IP address works. To use names, put both containers on a user-defined bridge network.