0
0
Dockerdevops~10 mins

DNS resolution between containers in Docker - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - DNS resolution between containers
Start Docker Network
Create Containers
Containers Join Network
Container A Queries DNS
Docker Embedded DNS Resolves Name
IP Address Returned to Container A
Container A Connects to Container B Using IP
Docker creates a network where containers join and can resolve each other's names via embedded DNS, allowing communication by container names.
Execution Sample
Docker
docker network create mynet

docker run -d --name containerA --network mynet nginx

docker run -d --name containerB --network mynet nginx

docker exec containerA ping -c 1 containerB
Create a network, start two containers on it, then ping containerB from containerA using its name.
Process Table
StepActionCommand/QueryResultNotes
1Create networkdocker network create mynetNetwork 'mynet' createdCustom bridge network allows DNS resolution
2Start containerAdocker run -d --name containerA --network mynet nginxcontainerA runningAttached to 'mynet'
3Start containerBdocker run -d --name containerB --network mynet nginxcontainerB runningAttached to 'mynet'
4Ping containerB from containerAdocker exec containerA ping -c 1 containerBPing successful, IP resolvedDNS resolved 'containerB' to IP inside 'mynet'
5ExitPing command endsPing command exits with successCommunication verified
💡 Ping command completes successfully, confirming DNS resolution and connectivity
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
Network 'mynet'NoneCreatedCreatedCreatedCreatedCreated
containerA statusNoneNoneRunningRunningRunningRunning
containerB statusNoneNoneNoneRunningRunningRunning
DNS resolution in containerANoneNoneNoneNonecontainerB -> IPcontainerB -> IP
Key Moments - 2 Insights
Why can't containers resolve each other's names without a user-defined network?
By default, containers on the default bridge network cannot resolve names because Docker's embedded DNS only works on user-defined networks, as shown by step 1 creating 'mynet' enabling DNS.
What does the ping command inside containerA actually test?
It tests both DNS resolution of 'containerB' to its IP and network connectivity, as seen in step 4 where ping succeeds after DNS resolves the name.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step is the network 'mynet' created?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Check the 'Action' and 'Result' columns in the first row of the execution table.
According to the variable tracker, when does containerB start running?
AAfter Step 3
BAfter Step 2
CAfter Step 1
DAfter Step 4
💡 Hint
Look at the 'containerB status' row and see when it changes to 'Running'.
If containerA was not attached to 'mynet', what would happen to the ping command in step 4?
APing would succeed as usual
BPing would fail due to no DNS resolution
CPing would resolve IP but fail to connect
DPing would connect but not resolve IP
💡 Hint
Refer to the key moment about DNS resolution requiring user-defined networks.
Concept Snapshot
Docker DNS resolution between containers:
- Create a user-defined network (docker network create)
- Run containers attached to this network (--network option)
- Containers can resolve each other by name via Docker embedded DNS
- Use commands like ping containerName inside containers to test
- Default bridge network does NOT support this DNS resolution
Full Transcript
This visual execution shows how Docker enables DNS resolution between containers by using a user-defined network. First, a network named 'mynet' is created. Then two containers, containerA and containerB, are started and attached to 'mynet'. When containerA runs a ping command to 'containerB', Docker's embedded DNS resolves 'containerB' to its IP address within the network. The ping succeeds, confirming both DNS resolution and network connectivity. Variables like container statuses and network state are tracked step-by-step. Key points include the necessity of a user-defined network for DNS resolution and that the ping command tests both name resolution and connectivity.