📖 Scenario: You are setting up a small Docker network where two containers can find each other by name. This is like having two friends in a room who can call each other by their names instead of phone numbers.We will create two containers: a simple web server and a client that will ping the server using its container name. This shows how Docker's built-in DNS helps containers discover each other easily.
🎯 Goal: Build a Docker network with two containers named webserver and client. The client container will use the container name webserver to ping and confirm it can reach the webserver container using Docker's DNS service discovery.
📋 What You'll Learn
Create a Docker network named my_network
Run a container named webserver using the nginx image connected to my_network
Run a container named client connected to my_network
Use the ping command inside the client container to ping webserver
Display the ping result to confirm connectivity
💡 Why This Matters
🌍 Real World
In real projects, containers need to find and talk to each other easily. Docker's DNS service discovery lets containers use simple names instead of IP addresses, making communication reliable and easier to manage.
💼 Career
Understanding container networking and service discovery is essential for DevOps roles, cloud engineers, and developers working with microservices and container orchestration.
Progress0 / 4 steps
1
Create a Docker network
Create a Docker network called my_network using the command docker network create my_network.
Docker
Hint
Think of a Docker network as a private room where containers can talk to each other by name.
2
Run the webserver container on the network
Run a container named webserver using the nginx image connected to the my_network network with the command docker run -d --name webserver --network my_network nginx.
Docker
Hint
Use --name to name the container and --network to connect it to the network.
3
Run the client container on the network
Run a container named client connected to the my_network network using the alpine image with the command docker run -it --rm --name client --network my_network alpine sh. Then install ping inside the container with apk add --no-cache iputils.
Docker
Hint
The alpine image is small and you need to add ping manually using apk.
4
Ping the webserver container from the client
Inside the client container shell, run the command ping -c 3 webserver to send 3 ping requests to the webserver container and confirm connectivity.
Docker
Hint
If you see lines starting with PING webserver and replies, it means the client found the webserver by name.
Practice
(1/5)
1. What does Docker's built-in DNS service provide for containers on the same user-defined network?
easy
A. It assigns static IP addresses to containers.
B. It allows containers to find each other by their container names.
C. It disables network communication between containers.
D. It automatically updates container images.
Solution
Step 1: Understand Docker user-defined networks
User-defined networks enable containers to communicate using DNS names instead of IP addresses.
Step 2: Role of Docker DNS service
Docker's DNS service resolves container names to their current IP addresses on the same network.
Final Answer:
It allows containers to find each other by their container names. -> Option B
Quick Check:
Docker DNS = container name resolution [OK]
Hint: Containers use names, not IPs, to find each other on user networks [OK]
Common Mistakes:
Thinking Docker assigns static IPs by default
Believing DNS disables container communication
Confusing DNS with image updates
2. Which Docker command correctly creates a user-defined network named my-net to enable container DNS service discovery?
The correct command to create a network is docker network create <name>.
Step 2: Verify options
The options using 'docker network new', 'docker create network', and 'docker network build' have incorrect syntax; only docker network create my-net matches Docker syntax.
Final Answer:
docker network create my-net -> Option A
Quick Check:
Correct command = docker network create [OK]
Hint: Use 'docker network create' to make user-defined networks [OK]
Common Mistakes:
Using 'docker create network' instead of 'docker network create'
Confusing 'new' or 'build' with network creation
Omitting the network name
3. Given two containers 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?
medium
A. Ping succeeds showing db IP address and response time.
B. Ping fails with 'unknown host db' error.
C. Ping succeeds but shows IP of web container.
D. Ping command is not available inside containers.
Solution
Step 1: Understand container DNS on user-defined networks
Containers on the same user-defined network can resolve each other's names to IP addresses.
Step 2: Effect of ping command
Running ping from web to db will resolve db to its IP and send ICMP packets successfully if db is running.
Final Answer:
Ping succeeds showing db IP address and response time. -> Option A
Quick Check:
Container DNS + ping = success if target running [OK]
Hint: Ping container name on same network to test DNS and connectivity [OK]
Common Mistakes:
Assuming ping fails due to IP changes
Confusing container names with hostnames outside network
Believing ping is unavailable inside containers
4. You created two containers on a user-defined network, but web cannot resolve db by name. What is the most likely cause?
medium
A. The web container has no network interface.
B. The db container is stopped.
C. Docker daemon is not running.
D. Containers are on different Docker networks.
Solution
Step 1: Check network configuration
Containers must be on the same user-defined network for DNS name resolution to work.
Step 2: Analyze failure cause
If web cannot resolve db, they are likely on different networks, so DNS does not link them.
Final Answer:
Containers are on different Docker networks. -> Option D
Quick Check:
Different networks block DNS resolution [OK]
Hint: Ensure containers share the same user-defined network for DNS [OK]
Common Mistakes:
Assuming stopped container causes DNS failure (it causes ping failure)
Thinking Docker daemon status affects container DNS directly
Believing container can lack network interface if running
5. You want to deploy a multi-container app with a web server and database. You want the web server to always connect to the database by name, even if the database container restarts and gets a new IP. Which Docker setup ensures this?
hard
A. Run containers on separate networks and link them with --link option.
B. Use the default bridge network and hardcode the database container's IP address in the web server config.
C. Create a user-defined network and run both containers attached to it; use the database container's name in the web server config.
D. Assign static IPs manually to both containers on the default network.
Solution
Step 1: Understand service discovery with user-defined networks
User-defined networks provide automatic DNS resolution by container name, even if IPs change.
Step 2: Evaluate options for reliable name-based connection
Create a user-defined network and run both containers attached to it; use the database container's name in the web server config. uses user-defined network and container names, which is the recommended way for service discovery.
Step 3: Why other options fail
Default bridge network lacks automatic DNS; hardcoding IPs breaks if IP changes; --link is deprecated; static IPs are complex and error-prone.
Final Answer:
Create a user-defined network and run both containers attached to it; use the database container's name in the web server config. -> Option C
Quick Check:
User-defined network + container names = reliable service discovery [OK]
Hint: Use user-defined network and container names for stable service discovery [OK]
Common Mistakes:
Hardcoding IP addresses instead of using container names
Using default bridge network without DNS support
Relying on deprecated --link option
Manually assigning static IPs unnecessarily
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.