How Containers Communicate and Automate Microservices Deployment
Introduction
If you’re looking to understand Docker networking in depth and learn how to orchestrate multiple microservices using Docker Compose, this guide is for you. Whether you’re a developer, DevOps engineer, or AI/ML practitioner, mastering container networking is crucial for building scalable and automated development environments. This article is a textual deep dive into the key concepts covered in our latest Docker Masterclass 3, which is made available as part of the Open Learning Series by School of DevOps.
Understanding Docker Networking
Containers and Networking: The Basics
Docker containers operate in isolated environments, and networking is what enables them to communicate with each other. To facilitate this, Docker provides various networking models:
Bridge Network (default) – Allows containers on the same host to communicate.
Host Network – Directly attaches the container to the host network, bypassing isolation.
None Network – Completely disables networking.
Overlay Network – Connects containers across multiple hosts (used in Docker Swarm).
Macvlan & Ipvlan – Used in specialized scenarios for assigning direct network interfaces to containers.
⠀Each container has its own network namespace, isolating its network stack. The connection between containers and the host is made possible through virtual Ethernet (VETH) pairs, where:
Each container gets an interface (eth0).
A corresponding VETH interface exists on the host.
These interfaces are connected via a Linux network bridge, allowing communication.
⠀
How Containers Communicate
By default, Docker assigns an IP address to each container in the bridge network. Containers communicate via:
1 Container Name Resolution: Docker provides an internal DNS service where containers can resolve each other by name.
2 Network Bridges: Connects multiple containers within the same network namespace.
3 Docker Network Commands:
docker network ls – Lists all networks.
docker network create – Creates a custom network.
docker run --network=<network-name> – Assigns a container to a network.
⠀
Real-World Example: The Voting App Stack
To illustrate container networking, we use an example voting app that consists of five microservices:
Vote: Frontend service where users vote.
Redis: Stores the votes.
Worker: Processes votes from Redis and writes them to the database.
DB: PostgreSQL database storing results.
Result: Frontend displaying vote results.
For these services to communicate efficiently, we use Docker Compose.
Docker Compose: Automating Microservices Deployment
What is Docker Compose?
Docker Compose is a tool that allows you to define and run multi-container Docker applications. Instead of running multiple docker run commands, you define your entire stack in a docker-compose.yml file and launch it with a single command.
Writing a Docker Compose File
A typical docker-compose.yml file follows this structure:
version: '3.9'
services:
vote:
image: schoolofdevops/vote
ports:
- "8000:80"
networks:
- app_network
redis:
image: redis:alpine
networks:
- app_network
worker:
image: schoolofdevops/worker
depends_on:
- redis
- db
networks:
- app_network
db:
image: postgres:9.4
environment:
POSTGRES_PASSWORD: example
networks:
- app_network
result:
image: schoolofdevops/result
ports:
- "8001:80"
depends_on:
- db
networks:
- app_network
networks:
app_network:
driver: bridge
Key Features of Docker Compose
Service Discovery: Containers can refer to each other by service name.
Networking: All services are assigned to app_network ensuring connectivity.
Automated Deployment:
docker-compose up -d – Brings up the entire stack.
docker-compose ps – Lists running services.
docker-compose logs – Shows container logs.
docker-compose down – Shuts down all containers.
⠀
Debugging and Troubleshooting
If a service fails to start, use:
docker logs <container-name> – To view logs of a specific container.
docker-compose exec <service-name> bash – To enter a running container.
nslookup <service-name> – To check if service discovery is working.
⠀
Example: Debugging a Redis Connection Issue
docker-compose exec vote nslookup redis
Server: 127.0.0.11
Address: 127.0.0.11#53
Name: redis
Address: 172.18.0.2
If the lookup fails, ensure that the Redis container is running and connected to the correct network.
Why Docker Compose Matters for DevOps
Benefits of Using Docker Compose:
Rapid Environment Setup: Ideal for local development and testing.
Infrastructure as Code: Defines microservices in a reproducible way.
Seamless Collaboration: Developers can spin up identical environments quickly.
Supports AI/ML Workloads: Can be used to containerize ML models and tools.
⠀If you’re deploying beyond a single host, Kubernetes becomes the next step, but for local development, Docker Compose is an essential tool.
Conclusion
Docker networking and Docker Compose provide the foundation for building robust, scalable containerized applications. By understanding how containers communicate, utilizing bridges, and automating service orchestration, you can create efficient DevOps workflows that streamline development and deployment.
💡 Try It Yourself! Clone the voting app and experiment with Docker Compose to see how microservices interact in a real-world scenario.
🚀 Stay tuned for the next session where we dive into Kubernetes !
Share this post