Container Networking
How containers get IPs, talk to each other, and reach the outside world.
Theory
Docker provides several network drivers:
- bridge (default) — a private internal network; containers get IPs and can reach each other. On a user-defined bridge, Docker adds DNS so containers resolve each other by name.
- host — the container shares the host’s network stack (no isolation, no port mapping)
- none — no networking
- overlay — spans multiple hosts (Swarm/clusters)
Port publishing (-p host:container) exposes a container port to the host.
Containers on the same user-defined bridge talk directly by name; the default
bridge requires --link (legacy) or IPs.
Real-World Example
docker network create app
docker run -d --name db --network app postgres:16
docker run -d --name api --network app -e DB=db:5432 myapi
# 'api' reaches the database at host 'db' over the 'app' network
docker run -d -p 8080:80 --network app nginx # publish to host:8080
docker network inspect app # see attached containers + IPs Hands-On Exercise
- Create a user-defined bridge and attach two containers that talk by name.
- Explain the difference between bridge and host networking.
- Publish a container port to the host and verify it from a browser/curl.
- When would you use an overlay network?
Cheat Sheet▾
| Driver / cmd | Purpose |
|---|---|
| bridge | Default private network |
| user-defined bridge | Adds name-based DNS |
| host | Share host network stack |
| none | No networking |
| overlay | Multi-host (clusters) |
-p host:container | Publish a port |
docker network create/inspect | Manage networks |
Common Interview Questions▾
How do containers on a user-defined bridge find each other?
Docker’s built-in DNS resolves container names to their IPs on that network, so they connect by name (e.g. db:5432) without hardcoding addresses.
What's the difference between bridge and host networking?
Bridge gives the container its own isolated network namespace with a private IP and port mapping. Host shares the host’s network stack directly — no isolation and no port publishing needed.
Official Documentation
📝 My notes on this topic
Auto-saves as you type