Container Networking Basics
What you'll learn: How containers communicate with each other and the outside world through virtual networks, bridges, and port mapping.
When you launch a container, it doesn't magically connect to the internet or other containers. Behind the scenes, the container runtime creates a virtual network infrastructure that enables communication while maintaining isolation.
How Containers Get Network Access
Think of container networking like an apartment building. Each apartment (container) has its own address within the building, and the building itself has a street address for the outside world.
When a container starts, it gets:
- Its own network namespace — a private networking environment with its own network interfaces, routing tables, and IP addresses
- A virtual network interface — like a virtual ethernet cable connecting it to a bridge
- An internal IP address — typically from a private range like 172.17.0.0/16
The Bridge Network
The bridge acts like a virtual switch connecting all containers on the same host. Containers on the same bridge can talk to each other using their internal IP addresses. The bridge itself connects to the host's physical network interface, allowing containers to reach the outside world through Network Address Translation (NAT).
Port Mapping
By default, containers are isolated from external networks. To expose a service running inside a container (say, a web server on port 80), you use port mapping. This binds a port on the host (like 8080) to a port in the container (80), creating a tunnel through the isolation boundary.
For example, traffic to hostIP:8080 gets forwarded to containerIP:80.
Key Takeaway: Containers communicate through virtual networks managed by the runtime, using bridges to connect containers and port mapping to expose services to the outside world, all while maintaining namespace isolation.