📖 Scenario: You are setting up two Docker containers that need to talk to each other on the same network. One container will run a simple web server, and the other will send a request to that server.
🎯 Goal: Build two Docker containers connected on the same network so that one container can successfully send a request to the other container's web server.
📋 What You'll Learn
Create a Docker network called my_network
Create a container named web_server running an nginx server connected to my_network
Create a container named client connected to my_network
From the client container, send a request to the web_server container using its container name
💡 Why This Matters
🌍 Real World
Containers often need to talk to each other in microservices or multi-component apps. Using Docker networks allows this communication easily.
💼 Career
Understanding container networking is key for DevOps roles managing containerized applications and orchestrations.
Progress0 / 4 steps
1
Create a Docker network
Create a Docker network called my_network using the docker network create command.
Docker
Hint
Use docker network create my_network to make a new network.
2
Run a web server container on the network
Run a container named web_server using the nginx image. Connect it to the my_network network using the --network option.
Docker
Hint
Use docker run -d --name web_server --network my_network nginx to start the web server container.
3
Run a client container on the network
Run a container named client using the alpine image. Connect it to the my_network network using the --network option. Start it with an interactive shell sh.
Docker
Hint
Use docker run -it --name client --network my_network alpine sh to start the client container with a shell.
4
Send a request from client to web_server
Inside the client container shell, install curl using apk add curl. Then send a request to http://web_server using curl and print the output.
Docker
Hint
Use apk add curl to install curl, then curl http://web_server to send the request.
Practice
(1/5)
1. What is the simplest way for two Docker containers to communicate on the same host?
easy
A. Use a shared volume for communication
B. Use the host's IP address directly inside containers
C. Run containers with --network=none and use port mapping
D. Connect both containers to the same user-defined Docker network
Solution
Step 1: Understand Docker networking basics
Docker containers on the same user-defined network can resolve each other by container name and communicate directly.
Step 2: Compare other options
Using host IP or port mapping is less direct and --network=none disables networking. Shared volumes are for file sharing, not network communication.
Final Answer:
Connect both containers to the same user-defined Docker network -> Option D
Quick Check:
Same network enables container communication [OK]
Hint: Use a user-defined network for easy container communication [OK]
Common Mistakes:
Using default bridge network without user-defined network
Trying to communicate via host IP without port mapping
Assuming shared volumes enable network communication
2. Which Docker command correctly creates a user-defined network named mynetwork?
easy
A. docker network create mynetwork
B. docker create network mynetwork
C. docker network new mynetwork
D. docker network add mynetwork
Solution
Step 1: Recall Docker network creation syntax
The correct command to create a network is docker network create <name>.
Step 2: Check other options for syntax errors
Options B, C, and D use incorrect verbs or commands not recognized by Docker.
Hint: Remember: 'docker network create' is the correct syntax [OK]
Common Mistakes:
Using 'docker create network' instead of 'docker network create'
Using 'docker network new' which is invalid
Trying 'docker network add' which does not exist
3. Given two containers web and db connected to the same user-defined network, what will be the output of docker exec web ping -c 1 db if db is running and reachable?
medium
A. ping: unknown host db
B. PING db (172.18.0.2): 56 data bytes\n64 bytes from 172.18.0.2: icmp_seq=0 ttl=64 time=0.123 ms\n\n--- db ping statistics ---\n1 packets transmitted, 1 packets received, 0% packet loss
C. connect: Network is unreachable
D. Request timed out
Solution
Step 1: Understand container DNS resolution on user-defined networks
Containers on the same user-defined network can resolve each other by container name, so 'db' resolves to its IP.
Step 2: Predict ping command output
Since 'db' is running and reachable, ping will succeed with a response showing bytes received and 0% packet loss.
Final Answer:
Successful ping output with 1 packet transmitted and received -> Option B
Quick Check:
Same network + running container = ping success [OK]
Hint: Ping container name on same network to test connectivity [OK]
Common Mistakes:
Assuming default bridge network supports container name resolution
Expecting ping to fail without port mapping
Confusing network unreachable with DNS resolution failure
4. You created two containers on a user-defined network but web cannot reach db. Which command helps you debug the network connectivity issue?
medium
A. docker ps -a
B. docker container logs web
C. docker network inspect mynetwork
D. docker volume ls
Solution
Step 1: Identify the tool to check network details
docker network inspect shows connected containers and network settings, useful for debugging connectivity.
Step 2: Evaluate other commands
docker container logsdocker ps -a lists containers; docker volume ls lists volumes, none show network details.
Final Answer:
docker network inspect mynetwork -> Option C
Quick Check:
Inspect network to debug connectivity [OK]
Hint: Use 'docker network inspect' to check container connections [OK]
Common Mistakes:
Checking container logs instead of network info
Listing volumes which are unrelated
Assuming 'docker ps' shows network details
5. You want two containers, app and cache, to communicate securely without exposing ports to the host. Which approach achieves this?
hard
A. Connect both containers to the same user-defined network and communicate via container names
B. Expose ports on both containers and use localhost to connect
C. Use host networking mode for both containers
D. Share a volume and write/read data files for communication
Solution
Step 1: Understand secure container communication
Using a user-defined network allows containers to communicate internally without exposing ports to the host, keeping communication private.
Step 2: Evaluate other options for security
Exposing ports or using host networking exposes services outside the host or risks port conflicts. Sharing volumes is not network communication.
Final Answer:
Connect both containers to the same user-defined network and communicate via container names -> Option A
Quick Check:
User-defined network keeps communication internal and secure [OK]
Hint: Use user-defined network to avoid exposing ports externally [OK]
Common Mistakes:
Exposing ports unnecessarily to the host
Using host network mode which is less secure
Confusing volume sharing with network communication
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.