Container network namespaces
Why localhost changes meaning
A Linux network namespace gives processes a distinct view of network interfaces, IP addresses, routes, sockets and firewall state. Two processes in different namespaces may both listen on TCP 8080 without competing for the same socket. In an ordinary isolated container, 127.0.0.1 refers to that container, not the host. A process using the host network namespace is an exception. Imagine a host running a metrics listener on 127.0.0.1:8080 and a container running an order API on 127.0.0.1:8080. The addresses look identical, but a request from the container reaches its own API. Namespace isolation is not a complete security boundary; process privileges and shared resources also matter.
Follow a packet through a bridge
A virtual Ethernet pair (veth) resembles a short Ethernet cable: a frame entering one end appears at the other. In our fictional Linux bridge design, container A owns 10.53.1.10/24 and B owns 10.53.1.11/24; each has a veth peer attached to a host bridge at 10.53.1.1. A resolves B with ARP and the bridge forwards the frame toward B. For an off-subnet destination, A sends to its default gateway at 10.53.1.1; the host needs forwarding, a route and permission. A typical Docker bridge uses source masquerading for external access; a routed design need not do so. User-defined bridges also provide container-name discovery. An overlay can carry traffic between hosts by encapsulating it over an underlay; it needs working underlay reachability and suitable MTU. Not every container platform uses a bridge or overlay.
A 10.53.1.10 -- veth -- bridge 10.53.1.1 -- veth -- B 10.53.1.11A loopback: 127.0.0.1 (A only)Host loopback: 127.0.0.1 (host only)Listening is different from publishing
Suppose A now listens on 0.0.0.0:8080 inside its namespace. A fictional mapping binds host 127.0.0.1:18080 to A:8080. In this model, a host-local client can use the mapping, but a remote client cannot use that loopback-only host address. Publishing without an explicit host address commonly exposes the port on host interfaces; exact behavior depends on runtime, routing and firewall settings. A mapping to A:8080 does not fix a process listening only on A’s loopback: the translated destination is A’s interface IP. An EXPOSE declaration is image metadata, not proof of publishing. Never treat a mapping as application authentication. Check the real runtime version and authorized access paths before relying on exposure restrictions.
Diagnose one boundary at a time
Use the diagram to identify the namespace in which a test would run. First check whether the intended process listens on the target address and port, then whether its interface and route exist, then bridge membership and host forwarding, and finally publishing and policy. A host socket listing alone may miss a container listener. If A reaches B by IP but not name, investigate discovery instead of changing routing. If local publication works but remote access fails, verify whether local-only binding is intentional before proposing any change. All readings here are invented, not actual command output. Do not mount a host Docker socket or use privileged shared containers for this exercise.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Container network namespaces Quiz
Reason about namespaces, Service forwarding and network policy.
A and the host both listen on their own 127.0.0.1:8080. Which listener does A reach using that address?
What does a veth pair provide?
A sends to an off-subnet IP using the example bridge. What is its next hop?
A listens only on its own loopback. Will mapping host port 18080 to A’s interface IP:8080 alone fix access?
IP access works but the container name fails. Which boundary is most relevant first?