Connect Docker Containers Across Hosts Using Ambassador
This guide explains how to use the Ambassador container pattern to enable Docker containers on different hosts to communicate without needing to know each other's IP addresses, illustrated with a step‑by‑step Redis example.
When two Docker containers run on the same host, they can communicate directly using the --link option.
To enable container communication across different hosts, containers normally need to know the IP address of the remote host.
The Ambassador container pattern allows containers to communicate without knowing each other's host IP addresses.
An Ambassador is itself a Docker container that provides a forwarding service.
Example using the Redis image:
Goal
(redis client) → (redis‑ambassador) —network→ (redis‑ambassador) → (redis server)
Steps
(1) On the server host, create a Redis server container: sudo docker run -d -name redis-server crosbymichael/redis (2) Create an Ambassador container on the server host that links to the Redis server and listens on the local port 6379:
sudo docker run -d -link redis-server:redis -name redis_ambassador -p 6379:6379 svendowideit/ambassador(3) On the client host, create a client Ambassador container, pointing it to the server host’s address (replace x.x.x.x with the actual IP):
sudo docker run -d -name redis_ambassador -expose 6379 -e REDIS_PORT_6379_TCP=tcp://x.x.x.x:6379 svendowideit/ambassador(4) Finally, run a client container to test the connection; accessing port 6379 on the client actually reaches the Redis instance inside the server container:
sudo docker run -i -t --rm -link redis_ambassador:redis relateiq/redis-cliInside the client container: redis 172.17.0.160:6379> ping Response:
PONGSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java High-Performance Architecture
Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
