CoursesDocker for beginnersData, networking & Compose

Container networking basics

bridge, host, none, and publishing ports.

Beginner12 min · lesson 13 of 16
In plain terms
Containers on the same named network are like phones saved in each other’s contacts — they dial each other by name instead of memorizing numbers (IP addresses) that keep changing. Publishing a port is like giving one of them a public phone number the outside world can call.

When Docker starts, it creates networks, and every container joins one and gets its own IP address on it. The default network is a bridge — a private virtual network on the host — so containers can reach each other by IP, and reach the outside world, but are isolated from your host’s network unless you publish a port. Understanding the drivers is what turns container networking from mysterious to obvious.

The network drivers you meet first
the common ones
bridge
default — private, isolated network
host
share the host’s network directly
none
no networking at all
bridge is the default and what you use most. host removes isolation (fast, but no port mapping). none is for containers that should have no network.

User-defined networks give you names

The default bridge lets containers talk by IP, which is fragile because IPs change. Create your own bridge network instead and Docker adds automatic DNS: containers on that network can reach each other by container name. This is the key to multi-container apps — the web container connects to “db” by name, and Docker resolves it, no IPs or links required.

terminal
$ docker network create appnet
$ docker run -d --name db --network appnet postgres:16
$ docker run -d --name web --network appnet myapp:1.0
$ docker exec web ping -c1 db # resolves by name, thanks to the user-defined network
PING db (172.18.0.2): 56 data bytes

Publishing versus internal traffic

Keep two ideas separate. Containers on the same user-defined network reach each other directly by name and port, with no -p needed — that traffic never touches the host. Publishing with -p is only for exposing a container to the outside world through the host. So a database that only your app talks to should not be published at all; only the public-facing web server needs -p.

Use a user-defined network for multi-container apps
The default bridge deliberately has no automatic DNS, so containers there cannot find each other by name — a frequent beginner snag. Always create a user-defined network (or use Compose, which makes one for you) so name resolution just works.