0
0
Dockerdevops~10 mins

Container to container communication in Docker - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Container to container communication
Start Containers
Containers on same network?
NoCreate Network
Yes
Container A sends request
Network routes request
Container B receives request
Container B sends response
Container A receives response
Communication successful
Containers must be on the same Docker network to communicate. One container sends a request, the network routes it to the other container, which responds back.
Execution Sample
Docker
docker network create mynet

docker run -d --name containerA --network mynet alpine sleep 1000

docker run -d --name containerB --network mynet alpine sleep 1000

docker exec containerA ping -c 2 containerB
Create a network, start two containers on it, then ping from containerA to containerB to test communication.
Process Table
StepActionCommand/ConditionResult/Output
1Create networkdocker network create mynetNetwork 'mynet' created
2Start containerA on mynetdocker run -d --name containerA --network mynet alpine sleep 1000containerA running
3Start containerB on mynetdocker run -d --name containerB --network mynet alpine sleep 1000containerB running
4Ping containerB from containerAdocker exec containerA ping -c 2 containerB2 packets transmitted, 2 received, 0% packet loss
5Communication successPing replies receivedContainer to container communication works
💡 Ping test completes successfully, confirming communication between containers on the same network
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
Network 'mynet'NoneCreatedCreatedCreatedCreatedCreated
containerA statusNot runningNot runningRunningRunningRunningRunning
containerB statusNot runningNot runningNot runningRunningRunningRunning
Ping resultNo pingNo pingNo pingNo ping2 packets receivedSuccess
Key Moments - 3 Insights
Why do containers need to be on the same network to communicate?
Containers communicate over Docker networks. If they are on different networks, Docker isolates them, so the ping in step 4 would fail. See execution_table step 4 where ping succeeds only because both containers share 'mynet'.
What happens if you try to ping a container by name but it is not on the same network?
The ping command will fail because Docker's internal DNS cannot resolve container names across different networks. This is why in step 4, ping works only when both containers are on 'mynet'.
Why do we use 'docker exec' to run ping inside containerA?
'docker exec' runs commands inside a running container. Here, it lets us test communication from containerA's perspective by running ping inside it, as shown in step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the status of containerB after step 3?
APaused
BNot running
CRunning
DExited
💡 Hint
Check the 'containerB status' row in variable_tracker after step 3
At which step does the network 'mynet' get created?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look at the 'Action' and 'Command' columns in execution_table for network creation
If containerA was not connected to 'mynet', what would happen at step 4?
APing would fail because containerB is unreachable
BPing would succeed with 0% packet loss
CPing would succeed but with packet loss
DPing would not run because containerA is stopped
💡 Hint
Refer to key_moments about network isolation and ping failure
Concept Snapshot
Docker containers communicate over networks.
Containers must be on the same Docker network to reach each other by name.
Use 'docker network create' to make a network.
Start containers with '--network <name>' to connect them.
Use 'docker exec <container> ping <other_container>' to test communication.
If containers are on different networks, communication fails.
Full Transcript
To enable communication between Docker containers, they must be connected to the same Docker network. First, create a network using 'docker network create'. Then start containers with the '--network' option to attach them to this network. Inside one container, you can run commands like ping to the other container by its name. This works because Docker provides internal DNS resolution within the same network. If containers are on different networks, they cannot see each other and communication fails. Using 'docker exec' lets you run commands inside a running container to test connectivity. This step-by-step process ensures containers can talk to each other securely and easily.