How Containers Communicate with Each Other?
Container communication is one of the most important concepts in Docker, Kubernetes, microservices architecture, and cloud-native applications.
Modern applications are composed of multiple services running inside containers:
API Gateway
Payment Service
User Service
Notification Service
MySQL Database
Redis Cache
Kafka
These containers must communicate securely, efficiently, and reliably.
Why Container Communication Matters
In monolithic applications, everything runs in one process.
In microservices architecture:
- Each service runs independently
- Each service has its own container
- Services communicate over networks
Without networking:
- API Gateway cannot call services
- Services cannot access databases
- Redis cannot cache data
- Notifications cannot be sent
Real-Time Production Example
Consider a global learning platform serving users from USA, UK, and India.
Frontend
|
API Gateway
|
+-----------------------------+
| | |
v v v
Course Interview Payment
Service Service Service
|
v
MySQL Database
All these services communicate internally using Docker networking.
High-Level Container Communication Architecture
+------------------------------------------------------+
| Docker Host |
| |
| +----------------------------------------------+ |
| | Docker Network | |
| | | |
| | API Gateway <-----> Payment Service | |
| | | | |
| | +---------> MySQL Database | |
| | | |
| +----------------------------------------------+ |
| |
+------------------------------------------------------+
How Docker Networking Works Internally
Docker uses:
- Linux network namespaces
- Virtual Ethernet interfaces (veth pairs)
- Linux bridges
- iptables NAT rules
- DNS-based service discovery
Internal Networking Flow
Container A
|
v
Virtual Ethernet Pair
|
v
Docker Bridge Network
|
v
Container B
Default Docker Bridge Network
By default, Docker creates a bridge network called:
docker0
Containers connected to this bridge can communicate internally.
Container Communication Methods
- Container IP Communication
- Container Name Communication
- Docker Compose Service Discovery
- Overlay Networking
- Kubernetes Service Communication
1. Communication Using Container IP
Every container receives an internal IP address.
Example
Container A:
172.18.0.2
Container B:
172.18.0.3
Containers communicate using these IP addresses.
Example
http://172.18.0.3:8080
Problem
- IPs can change
- Not production friendly
- Hard to maintain
2. Communication Using Container Names
Docker provides internal DNS-based service discovery.
Create Network
docker network create app-network
Start Containers
docker run -d --name mysql \
--network app-network mysql
docker run -d --name payment-service \
--network app-network payment-service
payment-service can access MySQL using:
mysql:3306
Docker automatically resolves:
mysql
to container IP internally.
Internal DNS Flow
payment-service
|
v
Requests "mysql"
|
v
Docker Internal DNS
|
v
Returns MySQL Container IP
Why Container Name Communication is Better
- Stable service names
- Easier maintenance
- Dynamic IP handling
- Production-ready
3. Docker Compose Service Communication
Docker Compose automatically creates a shared network.
Example
services:
payment-service:
image: payment-service
mysql:
image: mysql
payment-service can directly connect using:
mysql:3306
Docker Compose Internal Communication
Docker Compose Network
|
+-------------------+
| |
v v
payment-service mysql
Spring Boot Database Example
spring.datasource.url=jdbc:mysql://mysql:3306/payment_db
Here:
mysql
refers to Docker Compose service name.
4. Overlay Network Communication
Overlay networking allows containers on different servers to communicate.
Overlay Architecture
Server 1
|
Container A
|
Overlay Network
|
Container B
|
Server 2
Used in:
- Docker Swarm
- Kubernetes
- Cloud-native distributed systems
5. Kubernetes Container Communication
Kubernetes networking is more advanced than basic Docker networking.
Pod
|
Kubernetes Service
|
Cluster Networking
|
Another Pod
Kubernetes provides:
- Cluster-wide DNS
- Service discovery
- Load balancing
- Overlay networking
Container Communication Flow in Production
User Request
|
v
Load Balancer
|
v
API Gateway Container
|
+---------------------------+
| |
v v
Payment Service Interview Service
|
v
MySQL Database
How Docker Creates Networking Internally
Step 1
Create Network Namespace
Step 2
Create Virtual Ethernet Pair
Step 3
Attach to Docker Bridge
Step 4
Assign Container IP
Step 5
Configure DNS Resolution
Virtual Ethernet Pair (veth)
Docker creates virtual network interfaces.
Container Interface <----> Host Interface
One side exists inside container namespace, another side connects to Docker bridge.
Docker Bridge Internals
Container A
|
veth pair
|
docker0 bridge
|
veth pair
|
Container B
docker0 behaves like a virtual switch.
How Containers Access Internet
Container
|
Docker Bridge
|
NAT using iptables
|
Host Network
|
Internet
Docker uses NAT to allow internet access.
Port Mapping
Containers use internal ports.
Example
docker run -p 8080:80 nginx
Meaning
Host Port: 8080
Container Port: 80
Production Example
docker run -d \
--name api-gateway \
-p 9090:9090 \
--network app-network api-gateway
Communication Between Microservices
Example
payment-service
|
v
Calls:
http://notification-service:8085/send
Docker DNS resolves:
notification-service
to actual container IP.
Container Communication in Docker Compose
services:
api-gateway:
image: api-gateway
payment-service:
image: payment-service
notification-service:
image: notification-service
Services communicate internally:
http://payment-service:8084
http://notification-service:8085
Production Networking Best Practices
- Use user-defined bridge networks
- Avoid hardcoded IP addresses
- Use container/service names
- Separate frontend/backend networks
- Limit exposed ports
- Use TLS between services
- Use service discovery
- Use network segmentation
Security Best Practices
- Expose only required ports
- Use isolated internal networks
- Restrict external access
- Use firewall rules
- Encrypt service communication
Common Container Communication Problems
- DNS resolution failure
- Wrong network configuration
- Port conflicts
- Firewall blocking
- Container not on same network
- Incorrect service names
How to Debug Container Communication
List Networks
docker network ls
Inspect Network
docker network inspect app-network
Ping Another Container
docker exec -it payment-service ping mysql
Check DNS Resolution
docker exec -it payment-service nslookup mysql
Docker Communication vs Kubernetes Communication
| Feature | Docker | Kubernetes |
|---|---|---|
| Service Discovery | Basic DNS | Advanced DNS |
| Load Balancing | Limited | Built-in |
| Multi-host Networking | Overlay | Native Cluster Networking |
| Scalability | Moderate | Very High |
Production Kubernetes Example
API Gateway Pod
|
v
payment-service.default.svc.cluster.local
|
v
Payment Service Pod
Interview Answer
Containers communicate with each other using Docker networking. Docker creates virtual networks using Linux network namespaces, virtual Ethernet interfaces, bridges, and DNS-based service discovery.
Containers connected to the same Docker network can communicate using container names instead of IP addresses. Docker internally resolves service names to container IPs using built-in DNS.
In production microservices systems, user-defined bridge networks, Docker Compose, overlay networking, and Kubernetes networking are commonly used for secure and scalable container communication.
Quick Summary Table
| Concept | Purpose |
|---|---|
| Bridge Network | Single-host communication |
| Container DNS | Service discovery |
| Overlay Network | Multi-host communication |
| Port Mapping | External access |
| Docker Compose | Automatic networking |
Useful Internal Links
- Docker Interview Questions
- DevOps Interview Questions
- Kubernetes Interview Questions
- Microservices Interview Questions
- AWS Interview Questions
- Linux Interview Questions
Final Conclusion
Container communication is a foundational concept in Docker, Kubernetes, microservices, and cloud-native systems. Docker networking enables isolated, scalable, and secure communication between services using virtual networks, DNS-based discovery, and Linux networking technologies.
Understanding container communication is essential for designing production-ready distributed systems, scalable microservices architectures, CI/CD pipelines, and enterprise cloud platforms.