What if your containers could always find each other without you chasing IP addresses?
Why DNS resolution between containers in Docker? - Purpose & Use Cases
Imagine you have several containers running different parts of your app, like a web server and a database. You try to connect them by guessing IP addresses or writing them down manually.
This manual way is slow and frustrating because container IPs change every time you restart them. You end up chasing IPs like a detective, and your app breaks often.
DNS resolution between containers lets them find each other by simple names, like friends calling each other by name instead of phone numbers. Docker automatically handles this name-to-IP mapping for you.
docker run --name webapp --link db_container:db mywebapp
# Use IP 172.17.0.2 to connect to dbdocker network create mynet
docker run --net mynet --name db dbimage
docker run --net mynet --name webapp webimage
# Connect to 'db' by nameYour containers can talk to each other easily and reliably, making your app stable and easier to manage.
A web app container connects to a database container by simply using the database's container name, even if the database restarts and gets a new IP.
Manual IP management is error-prone and breaks easily.
DNS resolution lets containers find each other by name automatically.
This makes multi-container apps more stable and easier to run.
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
