0
0
Dockerdevops~10 mins

Container DNS and service discovery in Docker - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Container DNS and service discovery
Container A starts
Container A registers name in DNS
Container B starts
Container B queries DNS for Container A
DNS returns Container A IP
Container B connects to Container A using IP
Service communication established
Containers register their names in an internal DNS. Other containers query this DNS to find IPs and connect, enabling service discovery.
Execution Sample
Docker
docker network create mynet

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

docker run -it --rm --network mynet busybox nslookup web
Create a network, run a web container, then run a busybox container to look up the web container's IP via DNS.
Process Table
StepActionCommand/QueryResult/Output
1Create networkdocker network create mynetNetwork 'mynet' created
2Start web containerdocker run -d --name web --network mynet nginxContainer 'web' running with IP 172.18.0.2
3Start busybox containerdocker run -it --rm --network mynet busybox nslookup webQuery DNS for 'web'
4DNS responsenslookup webName: web Address: 172.18.0.2
5Connect to webbusybox connects to 172.18.0.2Connection successful
6Exitbusybox exitsContainer removed
💡 Busybox container exits after DNS query and connection test
Status Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
Network 'mynet'nonecreatedcreatedcreatedcreated
Container 'web' IPnone172.18.0.2172.18.0.2172.18.0.2172.18.0.2
DNS query resultnonenonequery sent172.18.0.2172.18.0.2
Key Moments - 3 Insights
Why can the busybox container find the web container by name?
Because both containers are on the same user-defined network 'mynet', Docker automatically provides DNS resolution for container names within that network, as shown in execution_table step 4.
What happens if containers are on different networks?
Containers on different networks cannot resolve each other's names via DNS, so the busybox container would fail to find 'web' by name, unlike in step 4 where they share 'mynet'.
Why does the busybox container get removed after exit?
Because it was run with --rm flag, which tells Docker to remove the container after it stops, as noted in execution_table step 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what IP address does the DNS return for the 'web' container at step 4?
A127.0.0.1
B192.168.1.1
C172.18.0.2
DNone
💡 Hint
Check the 'Result/Output' column at step 4 in the execution_table.
At which step does the busybox container send the DNS query for 'web'?
AStep 3
BStep 2
CStep 5
DStep 6
💡 Hint
Look at the 'Action' and 'Command/Query' columns in the execution_table.
If the network 'mynet' was not created, what would happen when running the web container?
AContainer would fail to start
BContainer would run but no DNS resolution for 'web'
CContainer would run but busybox cannot find 'web' by name
DContainer would run with default network and DNS resolution works
💡 Hint
Refer to variable_tracker and key_moments about network and DNS behavior.
Concept Snapshot
Docker containers on the same user-defined network can resolve each other's names via built-in DNS.
Create a network with 'docker network create'.
Run containers attached to this network.
Use container names to connect instead of IPs.
DNS resolves names to container IPs automatically.
This enables simple service discovery inside Docker networks.
Full Transcript
This visual execution shows how Docker containers use internal DNS for service discovery. First, a user-defined network 'mynet' is created. Then, a web container runs attached to 'mynet' and gets an IP address. Next, a busybox container runs on the same network and queries DNS for the name 'web'. The DNS returns the IP of the web container. Busybox uses this IP to connect to the web container successfully. Finally, busybox exits and is removed because it was run with the --rm flag. This demonstrates how containers discover each other by name using Docker's internal DNS on user-defined networks.