How Containers Communicate in Docker: Simple Explanation and Examples
Docker containers communicate using
networks that connect them. Containers on the same network can talk to each other using container names as hostnames. Ports can be exposed and mapped to allow communication between containers and the outside world.Syntax
Docker uses networks to enable communication between containers. You create a network, then run containers attached to it. Containers can reach each other by their container name or alias.
Key commands:
docker network create <network-name>: Creates a new network.docker run --network <network-name> --name <container-name> <image>: Runs a container on the specified network with a name.docker network ls: Lists all networks.docker network inspect <network-name>: Shows details of a network.
bash
docker network create my-net docker run -dit --name container1 --network my-net alpine sh docker run -dit --name container2 --network my-net alpine sh
Example
This example shows two containers communicating by pinging each other using container names on the same Docker network.
bash
docker network create my-net
docker run -dit --name container1 --network my-net alpine sh
docker run -dit --name container2 --network my-net alpine sh
docker exec container1 ping -c 3 container2Output
PING container2 (172.18.0.3): 56 data bytes
64 bytes from 172.18.0.3: seq=0 ttl=64 time=0.123 ms
64 bytes from 172.18.0.3: seq=1 ttl=64 time=0.110 ms
64 bytes from 172.18.0.3: seq=2 ttl=64 time=0.105 ms
--- container2 ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
Common Pitfalls
1. Containers not on the same network cannot communicate by name. They must share a network or use port mapping.
2. Forgetting to expose or publish ports when communicating with the host or external services.
3. Using the default bridge network limits name resolution; containers must use IP addresses or links.
bash
docker run -dit --name container1 alpine sh docker run -dit --name container2 alpine sh docker exec container1 ping -c 3 container2 # This will fail because container1 and container2 are on default bridge network without links or custom network. # Correct way: docker network create my-net docker network connect my-net container1 docker network connect my-net container2 docker exec container1 ping -c 3 container2
Quick Reference
| Concept | Description | Command Example |
|---|---|---|
| Create Network | Creates a user-defined network for containers | docker network create my-net |
| Run Container on Network | Runs container attached to a network with a name | docker run --network my-net --name c1 alpine sh |
| List Networks | Shows all Docker networks | docker network ls |
| Inspect Network | Shows details and connected containers | docker network inspect my-net |
| Ping Container | Test communication by container name | docker exec c1 ping -c 3 c2 |
Key Takeaways
Containers communicate best when attached to the same Docker network using container names.
User-defined bridge networks enable automatic DNS resolution between containers.
Default bridge network limits communication by name; use custom networks instead.
Expose and publish ports to allow communication with the host or external systems.
Always check network attachment if containers cannot reach each other.