Search code examples
dockerdnsdocker-composedocker-swarmdocker-swarm-mode

How Does One Use DNS in Docker to Reference All Replicated Containers by the Same DN?


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?


Solution

    • When you migrate to Docker swarm, you'll deal with docker swarm services.
    • Services may have multiple replicas. Each replica will have unique virtual IP. But that's not a problem, because each service may talk to other service by its service name.
    • A service name is registered in DNS of docker ingress network.

    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.