Reputation: 8701
I have some experience with Docker Compose and container linking. In a non-swarm environment, you could easily connect from, e.g, the web
container to the db_mysql
container using its name (for example, in PHP I can configure the MySQL connection to be:
$dsn = 'mysql:host=db_mysql;
I am having a hard time understanding how that works with Docker in Swarm mode, especially considering the "replicas" and "load balancing" mechanisms.
Let's say I have 5 different Docker Machines, each having a different public IP, participating in a Swarm. I also have a web
service and a db
service that's replicated across these 5 different machines (1 instance per each machine).
My question is: how do I make any of the 5 web
containers, communicate to any of the 5 db_mysql
containers without forcing these web
containers to have knowledge of any Docker Machine public IPs or the fact that these containers live within a Swarm?
Upvotes: 3
Views: 2065
Reputation: 263479
You use the service name. This will resolve in DNS to either a VIP or the 5 ip addresses (one for each replica) of the service. Under the covers, the VIP uses IPVS to round robin to one of the healthy replicas without suffering from stale DNS issues. You can also get all the replica IP addresses using tasks.service_name
even if you use the default VIP.
In Docker's DNS implementation, you can resolve the container name, and any network alias. The network alias includes the service name with DNSRR (used by docker-compose
without swarm). Or the service name resolves to a VIP in swarm mode. The hostname of the container does not resolve, likely because it can change outside of the control (and therefore knowledge) of the docker engine.
Upvotes: 5
Reputation: 33
Using Docker version 19.03.5 the correct DNS name to query in order to obtain all the IP addresses of the replica of a service is the following:
tasks.<service-name>
Upvotes: 0