Lesson 26 of 30
Compose resolving a service name to a container's IP feels like the whole story until a service has ten replicas instead of one — then 'resolve the name' quietly becomes 'resolve the name to whichever of ten, currently-changing IPs should handle this request.'
11 min read
Every option explains itself — why the right one is right, and why each wrong one is wrong. Miss one and it comes back later on a spaced-repetition schedule.
Question 1 of 3
In Compose, a service name resolves to one container's IP because there's usually one container per service. Why doesn't that same simple mapping work once an orchestrator runs multiple replicas of a service?
Hints without spoilers, and a plain explanation of why a wrong answer was wrong — answered only from this lesson. Free account, no card.
Sign up freeWhat interviewers actually ask about this, and what a strong answer has to cover.
A strong answer covers
Likely follow-ups
The Compose networking lesson earlier in this track covered the basic mechanism: on a user-defined bridge network, Docker runs an embedded DNS server, and a service name resolves directly to that one container's IP. This works cleanly because Compose almost always means one container per service — the name-to-IP mapping is simple because there's only ever one IP to map to.
Service discovery is what that same idea becomes once "one container" stops being true. An orchestrator (Swarm, Kubernetes, and similar) runs replicas — several interchangeable containers behind one logical service name — and replicas come and go constantly: scaled up, scaled down, replaced after a health check failure, rescheduled onto a different machine. Resolving a service name now has to answer a harder question: not "what's the IP," but "which of the currently healthy replicas should this particular request go to, right now." That's service discovery — DNS-by-name plus a live, constantly-updated membership list and a way to pick one, distributing load in the process.
Three replicas of "api" are healthy — the name could resolve to any one of them.
Two real mechanisms implement this, and interviews tend to probe whether you know they're different tools solving the same problem:
Service objects — a stable virtual IP and DNS name (api.namespace.svc.cluster.local, informally just api) backed by kube-proxy (or an equivalent), which continuously watches which pods currently match the Service's label selector and updates the routing accordingly as pods are created, destroyed, or fail health checks.Both do the same conceptual job Compose's embedded DNS does at a single-host scale — resolve a stable name to a currently-correct backend — but neither is "Compose's DNS, but bigger." They're built around a continuously changing membership list, which is a genuinely different problem than resolving a name to one container that doesn't move.
"How does a service find and talk to another service when there might be ten replicas of it, spread across different machines, changing over time?" is a step up from the Compose-level "how does DNS resolve a service name" question, and answering it well means naming the actual mechanism — a continuously-updated membership list feeding load-balanced resolution — rather than describing single-container DNS at a larger scale.