DNS Round Robin load balancing does not work in Compose mode

Hello, I am trying to load-balance my application in docker-compose via DNS. My application is only simple mail generator. So I need two programs, my application and SMTP/mail server. I use mailhog as mailserver.

I put only mailhog to the docker-compose.yaml file and build and run and scale to two instances:

version: “3.8”
services:
mailhog:
image: mailhog/mailhog:latest
restart: always
ports:
- “1025”
- “8025”

I run docker-compose, I scale the mailhog service to the two instances. Then, I use Dockerfile

FROM openjdk:8-jre-alpine
COPY target/spammer-jar-with-dependencies.jar .
CMD [“/usr/bin/java”, “-jar”, “spammer-jar-with-dependencies.jar”]

docker build …; docker run…;

also I need to connect my app container with my service:

docker network connect spammer_default infallible_clarke

I exec /bin/sh on infallible_clarke to log in and run commands:

/ # nslookup mailhog
nslookup: can’t resolve ‘(null)’: Name does not resolve
Name: mailhog
Address 1: 172.18.0.3 spammer_mailhog_2.spammer_default
Address 2: 172.18.0.2 spammer_mailhog_1.spammer_default

You can see, there are two DNS records and my app in infallible_clarke can use DNS round robin technique to load-balance/load-share requests.

However, in case if my docker compose is:

version: “3.8”
services:
mailhog:
image: mailhog/mailhog:latest
restart: always
ports:
- “1025”
- “8025”
spammer:
depends_on:
- mailhog
build:
context: “.”
dockerfile: .Dockerfile
image: spammer:latest
links:
- mailhog

does not work. I run this docker-compose.yaml, scaled service mailhog to two instances, then logged to the spammer service container and run command:

/ # nslookup mailhog
nslookup: can’t resolve ‘(null)’: Name does not resolve
Name: mailhog
Address 1: 172.18.0.2 spammer_mailhog_1.spammer_default

and you can see, there is only one A DNS record, so my application cannot use round robin load-balancing/sharing technique via DNS.

In swarm node when I add deploy configuration to docker-compose.yaml loadbalancing works, but I do not want to use swarm mode.

How to fix it?

docker version:
Docker version 19.03.8, build afacb8b @ Mac

1 Like