Container Networking

💤0
Lv 10 XP
← 📦 Containers & Kubernetes · Docker

Container Networking

Advanced ⭐ 120 XP ⏱ 18 min #docker#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
  1. Create a user-defined bridge and attach two containers that talk by name.
  2. Explain the difference between bridge and host networking.
  3. Publish a container port to the host and verify it from a browser/curl.
  4. When would you use an overlay network?
🧾Cheat Sheet
Driver / cmdPurpose
bridgeDefault private network
user-defined bridgeAdds name-based DNS
hostShare host network stack
noneNo networking
overlayMulti-host (clusters)
-p host:containerPublish a port
docker network create/inspectManage 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