I have a stack of microservices running on a single host. The stack basically consists of 2 stateless php web servers, and a stateful mysql database. I use a .env file to import the predefined container names to the php config files when the docker-compose up
command is executed. The containers communicate with each other using the predefined DNS names instead of IP addresses. This works when the stack is only running a single instance of each microservice.
However, I want to migrate to migrate to docker swarm. there will be multiple instances of each container spun up dynamically, and they will each get a unique DNS name; that breaks my DNS resolution, and therefore breaks my services as the containers can no longer communicate with each other (they can't resolve each other's IP address via DNS name).
What is the de facto way to deal with this?
For example, java service [stack_name]_app
may connect to service [stack_name]_db
by its name.
Docker swarm load balancer will send the queries to replicas of db service in round robin strategy.
And it doesn't matter how many replicas of db service exist. Service may be scaled up/down dynamically.