Container Network Communication
What you'll learn: How containers find each other by name, discover services automatically, and expose ports for external traffic.
The Communication Challenge
Once containers are running on a network, they need ways to talk to each other reliably and expose services to the outside world. Docker provides built-in mechanisms that make this seamless without requiring hardcoded IP addresses or complex routing rules.
DNS-Based Service Discovery
Within user-defined bridge networks (those you create, not the default bridge), Docker runs an embedded DNS server. Containers can reach each other simply by using container names as hostnames—Docker resolves these names to the correct container IP addresses automatically.
Analogy: Think of it like calling your colleague "Sarah" at work instead of memorizing her desk extension. The office directory (DNS) handles the lookup.
When you launch a container named web and another named api on the same user-defined network, web can connect to api using http://api:port directly. This discovery happens automatically—no configuration files needed.
Port Mapping for External Access
Containers run in isolated networks by default. To make a service accessible from your host machine or the internet, you map container ports to host ports using -p flags when starting containers.
For example, mapping host port 8080 to container port 80 means traffic hitting your host on 8080 gets forwarded into the container's port 80. This is how you expose web servers, APIs, and databases to external clients.
Container Name Resolution
Only works on user-defined networks—the default bridge network requires using legacy --link flags or direct IP addresses, which is why custom networks are strongly preferred for multi-container applications.
Key Takeaway: Docker's built-in DNS lets containers communicate by name on user-defined networks, while port mapping (-p) exposes services externally—no manual networking configuration required.