For future reference, to new viewers, starting from Compose file version 3.3 onwards, a service's deploy now supports 'endpoint_mode' options. Instead of the default 'vip' (virtual IP/proxy load balancer), option 'dnsrr' is now also available:
DNS round-robin (DNSRR) service discovery does not use a single virtual IP. Docker sets up DNS entries for the service such that a DNS query for the service name returns a list of IP addresses, and the client connects directly to one of these. DNS round-robin is useful in cases where you want to use your own load balancer, or for Hybrid Windows and Linux applications.
Meaning a command like 'nslookup'/'dig +short'/etc on your service (within the network) will now resolve to a list of container IPs, instead of the proxy load balancer in front of them.
nslookup <yourservice> | awk '/^Address: / { print $2 }' | xargs | sed -e 's/ /,/g'
can be used for a comma-seperated IP address string. A Java alternative:
Arrays.asList(java.net.InetAddress.getAllByName(<yourservice>));
You can adjust your application's code accordingly.
Based on this, you can implement your own behavior on awaiting each container's response and how to handle situations like not all containers replying within a specified time.
For compose file version 3.3+ compatibility, the docker engine version should be 17.06.0+.
Another approach is to resolve tasks.<service-name>. More information can be found in this service discovery question.