'Can't access docker hostet mongo replica set

My compose file:

version: '3'
services:
  mongo-1:
    image: mongo:latest
    volumes:
      - ./data/1/:/data/db
    ports:
      - "30001:27017"
    restart: always
    container_name: mongo-cluster-1
    command: mongod --replSet mongo-cluster-dev
  mongo-2:
    image: mongo:latest
    volumes:
      - ./data/2/:/data/db
    ports:
      - "30002:27017"
    restart: always
    container_name: mongo-cluster-2
    command: mongod --replSet mongo-cluster-dev
  mongo-3:
    image: mongo:latest
    volumes:
      - ./data/3/:/data/db
    ports:
      - "30003:27017"
    restart: always
    container_name: mongo-cluster-3
    command: mongod --replSet mongo-cluster-dev
networks:
  default:
    external:
      name: mongo-cluster-dev

my cluster config:

config = {
  "_id" : "mongo-cluster-dev",
  "members" : [{
    "_id" : 0,
    "host" : "mongo-1:27017"
  },
  {
    "_id" : 1,
    "host" : "mongo-2:27017"
  },
  {
  "_id" : 2,
  "host" : "mongo-3:27017"
  }]
}

The config works without problems. I cant access any of the 3 databases from outside via 192.168.21.155:30001(-3) But when I'am trying to connect with mongoose:

mongoose.connect("mongodb://192.168.21.155:30001,192.168.21.155:30002,192.168.21.155:30003/test?replicaSet=mongo-cluster-dev")

I got the following error:

MongoNetworkError: failed to connect to server [mongo-1:27017] on first connect [MongoNetworkError: getaddrinfo ENOTFOUND mongo-1 mongo-1:27017]

It seems to be a problem with dns? But when I use external IP and published ports in config, config fails.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source