📖 Scenario: You are working with Docker containers that need to communicate with each other on a custom network. Sometimes, containers cannot reach each other due to network misconfiguration. You want to set up a simple Docker network and containers, then check their connectivity to debug network issues.
🎯 Goal: Build a Docker setup with a custom network and two containers. Then, use Docker commands to check if the containers can ping each other, helping you understand and debug network connectivity problems.
📋 What You'll Learn
Create a custom Docker network named my_network
Run two containers named container1 and container2 attached to my_network
Use the alpine image for both containers
Check connectivity by pinging container2 from container1
Print the ping command output to verify connectivity
💡 Why This Matters
🌍 Real World
Docker containers often need to communicate in microservices or multi-container apps. Debugging network issues ensures services can talk to each other reliably.
💼 Career
DevOps engineers and developers use Docker networking daily to deploy and troubleshoot containerized applications in production and development.
Progress0 / 4 steps
1
Create a custom Docker network
Create a Docker network called my_network using the docker network create command.
Docker
Hint
Use the command docker network create my_network to create the network.
2
Run two containers on the custom network
Run two containers named container1 and container2 using the alpine image. Attach both containers to the my_network network. Use the docker run command with --network my_network and --name options.
Docker
Hint
Use docker run -dit --name container1 --network my_network alpine sh and similarly for container2.
3
Ping container2 from container1 to test connectivity
Use docker exec to run the ping -c 3 container2 command inside container1 to check if it can reach container2.
Docker
Hint
Run docker exec container1 ping -c 3 container2 to test connectivity.
4
Display the ping output
Run the command docker exec container1 ping -c 3 container2 and print its output to verify if the ping was successful.
Docker
Hint
The output should show lines starting with PING container2 and replies if successful.
Practice
(1/5)
1. What is the primary Docker command to list all networks and check their status?
easy
A. docker ps -a
B. docker container ls
C. docker network ls
D. docker inspect network
Solution
Step 1: Identify the command to list networks
The command docker network ls lists all Docker networks and their status.
Step 2: Differentiate from container commands
docker container ls and docker ps -a list containers, not networks. docker inspect network is incomplete and incorrect syntax.
Final Answer:
docker network ls -> Option C
Quick Check:
List networks = docker network ls [OK]
Hint: Use 'docker network ls' to see all networks quickly [OK]
Common Mistakes:
Confusing container list commands with network commands
Using incomplete or wrong inspect syntax
Trying to check networks with 'docker ps'
2. Which command syntax correctly runs a ping test inside a running container named webapp?
easy
A. docker exec webapp ping -c 4 google.com
B. docker run webapp ping google.com -c 4
C. docker exec -it ping webapp google.com
D. docker ping exec webapp google.com
Solution
Step 1: Understand the purpose of docker exec
docker exec runs commands inside an existing container. The syntax is docker exec [container] [command].
Step 2: Check the correct ping command format
ping -c 4 google.com sends 4 ping packets. docker exec webapp ping -c 4 google.com uses correct syntax. Options A, B, and C misuse docker run or command order.
Final Answer:
docker exec webapp ping -c 4 google.com -> Option A
Hint: Inspect bridge network to see container IPs and connections [OK]
Common Mistakes:
Confusing network inspect with container stats
Expecting logs or firewall info from network inspect
Misreading IP info as CPU or memory data
4. A container cannot reach the internet. You try docker exec app ping 8.8.8.8 but get 'ping: command not found'. What is the best next step?
medium
A. Run docker network disconnect bridge app to reset network.
B. Restart the Docker daemon with systemctl restart docker immediately.
C. Remove and recreate the container without any changes.
D. Install ping inside the container or use docker exec app apt-get update && apt-get install iputils-ping.
Solution
Step 1: Understand the error 'ping: command not found'
This means the ping tool is missing inside the container's OS.
Step 2: Fix by installing ping inside the container
Use package manager commands inside the container to install ping, e.g., apt-get install iputils-ping.
Final Answer:
Install ping inside the container or use docker exec app apt-get update && apt-get install iputils-ping. -> Option D
Quick Check:
Missing ping tool = install it inside container [OK]
Hint: If ping missing, install it inside container first [OK]
Common Mistakes:
Restarting Docker daemon without checking container tools
Removing container without diagnosing issue
Disconnecting network without confirming problem source
5. You have two containers, frontend and backend, on a user-defined Docker network mynetwork. The frontend cannot connect to backend by container name. What is the best way to fix this?
hard
A. Use the default bridge network instead of a user-defined network for better name resolution.
B. Ensure both containers are connected to the same user-defined network mynetwork and use container names for communication.
C. Add --link backend option when starting the frontend container to enable name resolution.
D. Restart Docker daemon to refresh network settings.
Solution
Step 1: Understand Docker network name resolution
Containers on the same user-defined network can resolve each other by container name automatically.
Step 2: Verify both containers are on the same network
If frontend cannot reach backend by name, likely they are not on the same network. Connect both to mynetwork.
Step 3: Avoid deprecated or less flexible methods
Using --link is deprecated and default bridge network does not support automatic name resolution.
Final Answer:
Ensure both containers are connected to the same user-defined network mynetwork and use container names for communication. -> Option B
Quick Check:
Same user network = container name resolution works [OK]
Hint: Connect containers to same user network for name resolution [OK]
Common Mistakes:
Using default bridge network expecting name resolution
Relying on deprecated --link option
Restarting Docker daemon without checking network setup
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.