Sub topic 5
Creating user-defined bridge networks
This chapter assumes you can run containers with docker run, use a shell, and understand IP addresses, subnets, and ports. You should also know that Docker's default bridge network exists, and that it has limits.
Why create your own bridge
A user-defined bridge is a virtual Layer 2 switch that Docker creates on the host. Containers attached to it get a private subnet and can reach each other. Compared with the default bridge, it adds three things:
- Containers resolve each other by name through Docker's embedded DNS.
- Containers are isolated from containers on other networks.
- You can attach and detach running containers without recreating them.
The default bridge offers none of these. Containers there can reach each other only by IP address, and every container without a --network flag lands on it together.
Create a network and attach containers
The commands below create a network, start a web server on it, and fetch its page from a second container by name.
docker network create app-net
docker run -d --name web --network app-net nginx:alpine
docker run --rm --network app-net curlimages/curl http://webThe third command prints the nginx welcome page. The hostname web works because the container name is registered with Docker's DNS on app-net. The next chapter covers how that lookup works.
If you omit --driver, Docker uses bridge. Each network gets its own subnet, taken from Docker's default address pool unless you set one.
Controlling the subnet
You can choose the address range yourself. This helps when the default pool overlaps with your VPN or office network.
docker network create \
--driver bridge \
--subnet 172.30.0.0/24 \
--gateway 172.30.0.1 \
custom-netYou can also pin a container to a fixed address with docker run --network custom-net --ip 172.30.0.10 .... Static addresses are only allowed on networks created with an explicit --subnet. In most cases a name is a better handle than a fixed IP.
Connecting and disconnecting live containers
A container can belong to several networks. Use docker network connect and docker network disconnect to change membership while it runs.
docker network create backend-net
docker network connect backend-net web
docker network disconnect backend-net webAfter connect, web has a second interface and a second IP. Containers on backend-net can now reach it by name as well.
How the pieces fit
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `network app-net already exists` | A network with that name was created earlier | Run `docker network ls`, then reuse it or remove it with `docker network rm app-net` |
| `could not resolve host: web` | The two containers are on different networks, or one is on the default bridge | Put both on the same user-defined network |
| `Pool overlaps with other one on this address space` | The `--subnet` collides with an existing Docker network | Choose a different range |
| `network app-net has active endpoints` | Containers are still attached | Stop or disconnect them, then remove the network |
Exercise
You start db on the default bridge and api on a user-defined network named app-net. Why can api not reach db with curl http://db, and what is the smallest change that fixes it?
Show answer
The two containers are on different networks, and the default bridge has no name-based DNS. Run docker network connect app-net db. Both containers are then on app-net, where db resolves by name.
Exercise
Write the command that creates a bridge network named lab-net with subnet 10.20.0.0/24.
Show answer
docker network create --subnet 10.20.0.0/24 lab-net. The bridge driver is the default, so --driver bridge is optional.