7.1.2.1 Service DNS Names
A focused guide to Service DNS Names, connecting core concepts with practical Docker and container operations.
Service DNS names are the automatically resolvable hostnames — matching container names by default — that Docker's embedded DNS server provides for containers attached to a shared user-defined network, enabling name-based communication without needing to track or hardcode specific IP addresses.
How Automatic DNS Resolution Works
Docker runs an internal DNS server for each user-defined network, automatically resolving container names to their current IP address on that network.
docker network create mynet
docker run -d --name db --network mynet postgres:16
docker run -d --name api --network mynet myapi:1.0
docker exec api nslookup db
This resolves db to whatever IP address that container currently has on mynet, handled automatically by Docker's embedded DNS.
Why This Doesn't Work on the Default Bridge Network
The default bridge network, unlike a user-defined network, does not provide this automatic DNS-based name resolution, which is one of the primary reasons user-defined networks are generally preferred for multi-container communication.
docker run -d --name db postgres:16
docker run -d --name api myapi:1.0
docker exec api nslookup db
Without an explicitly created, shared user-defined network, this resolution typically fails.
Resolution Updates Automatically as Containers Change
Because DNS resolution happens dynamically each time it's needed, a container recreated with a new IP address is still correctly resolved by name, without requiring any manual update to a hosts file or similar static configuration.
docker rm -f db
docker run -d --name db --network mynet postgres:16
docker exec api nslookup db
This resolves to the new container's current IP address automatically, despite the underlying address having changed.
Why Service DNS Names Matter
Automatic, dynamically updated name resolution is what makes container names a robust, reliable way to reference other services within a multi-container application, avoiding the fragility of hardcoded IP addresses that can change whenever a container is recreated.