Senica Gonzalez
Senica Gonzalez

Reputation: 8182

Docker Swarm ping by hostname incremental host.<id>

I have a service that requires that it can connect to the other instances of itself to establish a quorum.

The service has a environment variable like:

initialDiscoverMembers=db.1:5000,db.2:5000,db.3:5000

They can never find each other. I've tried logging into other containers and pinging other services by . like ping redis.1 and it doesn't work.

Is there a way in Docker (swarm) to get the incremental hostname working for connection as well? I looked at the endpoint_mode: dnsrr but that doesn't seem to be what I want.

I think I may have to just create three separate instances of the service and name it different things, but that seems so cumbersome.

Upvotes: 0

Views: 493

Answers (1)

Daniel Campos Olivares
Daniel Campos Olivares

Reputation: 2594

You cannot refer independently to each container using the incremental host.<id> since the DNS resolution on Swarm is done on a service-basis; what you can do is to add a hostname alias to each container based on its Swarm slot.

For example, right now you're using a db service, so you could add:

version: '3.7'

services:
    db:
        image: postgres
        deploy:
          replicas: 3
        hostname: "db-{{.Task.Slot}}"
        ports:
          - 5000:5432

In this case, since all the containers within each Swarm task are in the same network, you can address them by db-1, db-2 and db-3.

Upvotes: 1

Related Questions