📖 Scenario: You are working on a small Docker project where multiple containers communicate over a custom network. You want to inspect the network and debug connectivity issues between containers.
🎯 Goal: Learn how to create a Docker network, run containers attached to it, inspect the network details, and check container connectivity using Docker commands.
📋 What You'll Learn
Create a custom Docker network named my_custom_net
Run two containers named web and db attached to my_custom_net
Inspect the network my_custom_net to see connected containers
Use Docker commands to check connectivity between web and db
💡 Why This Matters
🌍 Real World
Docker networks help isolate and connect containers in real projects, making sure services can talk to each other securely and efficiently.
💼 Career
Understanding Docker networking and debugging is essential for DevOps roles to troubleshoot container communication issues and maintain healthy deployments.
Progress0 / 4 steps
1
Create a custom Docker network
Create a Docker network called my_custom_net using the docker network create command.
Docker
Hint
Use the command docker network create my_custom_net to create the network.
2
Run two containers attached to the custom network
Run two containers named web and db using the alpine image. Attach both containers to the my_custom_net network. Use the docker run command with --name and --network options. Run the containers in detached mode with -d.
Docker
Hint
Use docker run -d --name web --network my_custom_net alpine sleep 1000 and similarly for db.
3
Inspect the Docker network to see connected containers
Use the docker network inspect my_custom_net command to view details of the network and verify that both web and db containers are connected.
Docker
Hint
Use docker network inspect my_custom_net to see the network details.
4
Check connectivity between containers
Use the docker exec command to run a ping from the web container to the db container by its container name. Use docker exec web ping -c 3 db to send 3 ping packets.
Docker
Hint
Use docker exec web ping -c 3 db to test connectivity.
Practice
(1/5)
1. What does the Docker command docker network ls do?
easy
A. Creates a new Docker network
B. Shows running containers
C. Deletes a Docker network
D. Lists all Docker networks on the host
Solution
Step 1: Understand the command purpose
The command docker network ls is used to list networks, not containers or create/delete networks.
Step 2: Match command to action
Since it lists networks, it shows all Docker networks on the host machine.
Final Answer:
Lists all Docker networks on the host -> Option D
Quick Check:
docker network ls = list networks [OK]
Hint: Remember: 'ls' means list, so it lists networks [OK]
Common Mistakes:
Confusing network listing with container listing
Thinking it creates or deletes networks
Mixing up 'docker network ls' with 'docker ps'
2. Which of the following is the correct syntax to inspect details of a Docker network named my_network?
easy
A. docker network show my_network
B. docker inspect network my_network
C. docker network inspect my_network
D. docker network details my_network
Solution
Step 1: Recall Docker network inspection syntax
The correct command to inspect a network is docker network inspect <network_name>.
Step 2: Verify options
Only docker network inspect my_network matches the correct syntax exactly.
Final Answer:
docker network inspect my_network -> Option C
Quick Check:
docker network inspect = inspect network [OK]
Hint: Use 'docker network inspect' to see network details [OK]
Common Mistakes:
Swapping order of 'inspect' and 'network'
Using non-existent commands like 'show' or 'details'
Confusing with container inspect syntax
3. You run docker exec container1 ping -c 2 container2 to test connectivity. What output indicates successful communication?
medium
A. ping: unknown host container2
B. 2 packets transmitted, 2 received, 0% packet loss
C. 0 packets transmitted, 0 received, 100% packet loss
D. Error: container2 not found
Solution
Step 1: Understand ping output
Successful ping shows packets transmitted and received with 0% packet loss.
Step 2: Match output to success
2 packets transmitted, 2 received, 0% packet loss shows 2 packets sent and received with no loss, indicating success.
Final Answer:
2 packets transmitted, 2 received, 0% packet loss -> Option B
Quick Check:
Ping success = packets received with 0% loss [OK]
Hint: Look for 0% packet loss to confirm connectivity [OK]
Common Mistakes:
Confusing error messages with success
Ignoring packet loss percentage
Assuming any ping output means success
4. You try docker network inspect mynet but get an error: Error: No such network: mynet. What is the most likely cause?
medium
A. The network 'mynet' does not exist on the host
B. You need to run the command inside a container
C. Docker daemon is not running
D. You must use 'docker inspect network mynet' instead
Solution
Step 1: Analyze error message
The error says 'No such network', meaning Docker cannot find a network named 'mynet'.
Step 2: Identify cause
This usually means the network was not created or was removed.
Final Answer:
The network 'mynet' does not exist on the host -> Option A
Quick Check:
Error 'No such network' = network missing [OK]
Hint: Check network list before inspecting to avoid missing network errors [OK]
Common Mistakes:
Thinking command must run inside container
Assuming Docker daemon is stopped without checking
Using wrong command syntax
5. You have two containers, webapp and db, connected to a user-defined network appnet. You want to debug why webapp cannot reach db. Which sequence of commands helps you inspect and test the network connectivity?
hard
A. docker network inspect appnet; docker exec webapp ping -c 3 db
B. docker ps; docker logs webapp; docker network ls
C. docker network create appnet; docker exec db ping -c 3 webapp
D. docker exec webapp ifconfig; docker network rm appnet
Solution
Step 1: Inspect the network configuration
Use docker network inspect appnet to check if both containers are connected properly.
Step 2: Test connectivity from webapp to db
Run docker exec webapp ping -c 3 db to verify if webapp can reach db by name.
Final Answer:
docker network inspect appnet; docker exec webapp ping -c 3 db -> Option A
Quick Check:
Inspect network then ping from source to target [OK]
Hint: Inspect network first, then ping from source container [OK]
Common Mistakes:
Trying to ping from db to webapp instead of webapp to db
Creating network again instead of inspecting existing
Removing network before debugging
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.