DNS resolution between containers in Docker - Time & Space Complexity
When containers talk to each other, they use DNS to find addresses. Understanding how long this takes helps us know how fast containers connect.
We ask: What is the time complexity of resolving another container's name as more containers join the network?
Analyze the time complexity of the following Docker network DNS lookup process.
# Create a user-defined network
docker network create mynet
# Run containers attached to the network
for i in $(seq 1 5); do
docker run -d --net mynet --name container$i alpine sleep 1000
done
# From container1, ping container3 by name
docker exec container1 ping -c 1 container3
This code sets up a network, runs containers on it, and uses DNS to resolve container names.
Look for repeated steps in DNS resolution among containers.
- Primary operation: Hash table lookup of the container name in the DNS records.
- How many times: Constant time (O(1)), independent of the number of containers.
Docker's embedded DNS server uses a hash table for container name resolution, so lookup time remains constant as more containers join.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 1 lookup |
| 100 | About 1 lookup |
| 1000 | About 1 lookup |
Pattern observation: The number of operations remains constant regardless of the number of containers.
Time Complexity: O(1)
This means the time to find a container remains constant as more containers join the network.
[X] Wrong: "DNS lookup time grows linearly no matter how many containers there are."
[OK] Correct: Docker DNS uses hash tables for direct lookups, so time is constant and efficient even with many containers.
Knowing how DNS scales in container networks shows you understand real-world system behavior and helps you design better container setups.
"What if we used an external DNS cache for containers? How would the time complexity change?"
Practice
Solution
Step 1: Understand Docker networking basics
Docker provides a built-in DNS service for containers on the same user-defined network.Step 2: Recognize how containers resolve names
Containers use this DNS service to resolve other container names to their IP addresses automatically.Final Answer:
Docker's built-in DNS service -> Option CQuick Check:
Docker DNS = container name resolution [OK]
- Assuming default bridge network supports DNS resolution by name
- Thinking manual /etc/hosts editing is needed
- Using IP addresses instead of container names
mynetwork?Solution
Step 1: Recall Docker network creation syntax
The correct command to create a network isdocker network create <name>.Step 2: Match the command with options
docker network create mynetwork matches the correct syntax exactly.Final Answer:
docker network create mynetwork -> Option AQuick Check:
docker network create = create network [OK]
- Using 'docker create network' which is invalid syntax
- Confusing 'new' or 'build' with 'create'
- Omitting the network name
web and db on the same user-defined network, what will be the output of docker exec web ping -c 1 db if db is running?Solution
Step 1: Understand DNS resolution on user-defined networks
Containers on the same user-defined network resolve each other's names automatically.Step 2: Predict ping command output
Sincedbis running and reachable, ping will succeed and show reply details.Final Answer:
Successful ping output with reply from db's IP -> Option BQuick Check:
Same network + running container = ping success [OK]
- Expecting 'unknown host' on user-defined network
- Confusing default bridge network behavior
- Assuming container must expose ports for ping
ping db from web returns "unknown host db". What is the likely cause?Solution
Step 1: Recall default bridge network limitations
Default bridge network does not support automatic DNS resolution by container name.Step 2: Analyze the error message
"unknown host db" means DNS lookup failed, likely because containers are not on a user-defined network.Final Answer:
Containers are not on a user-defined network, so DNS resolution by name fails -> Option DQuick Check:
Default bridge lacks DNS by name [OK]
- Assuming default bridge network supports DNS names
- Ignoring network configuration
- Blaming container stopped state without checking
app and cache, to communicate by name. You create a user-defined network mynet. Which sequence of commands correctly sets this up?Solution
Step 1: Create a user-defined network
Usedocker network create mynetto create the network.Step 2: Run containers attached to the network
Start both containers with--network mynetto enable DNS resolution by name.Final Answer:
Create network first, then run containers with --network mynet -> Option AQuick Check:
User-defined network + containers attached = DNS by name [OK]
- Running containers before creating network
- Using default bridge network instead of user-defined
- Not attaching containers to the same network
Ready to prove it?
Pass the test and this pattern moves unknown
