← Back to Questions
Docker

How containers communicate with each other?

Learn How containers communicate with each other? with simple explanations, real-time examples, interview tips and practical use cases.

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.

Simple Definition: Containers communicate with each other using Docker networks, virtual networking, DNS-based service discovery, and internal IP routing.

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

  1. Container IP Communication
  2. Container Name Communication
  3. Docker Compose Service Discovery
  4. Overlay Networking
  5. 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

  1. Use user-defined bridge networks
  2. Avoid hardcoded IP addresses
  3. Use container/service names
  4. Separate frontend/backend networks
  5. Limit exposed ports
  6. Use TLS between services
  7. Use service discovery
  8. 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

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.

Why this Docker question is important?

This interview question helps candidates understand real-time backend development concepts, practical problem solving, coding fundamentals, system design basics and production-ready application behavior.

Practice this question carefully for Java backend roles, Spring Boot developer interviews, microservices interviews, company interviews and full-stack developer preparation.

About the Author

Naresh Kumar is a Senior Java Backend Engineer with experience building enterprise applications using Java, Spring Boot, Microservices, Docker, Kubernetes and Cloud technologies.