2

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.

M Reza
  • 18,350
  • 14
  • 66
  • 71
robert
  • 797
  • 3
  • 9
  • 23
  • 1
    If you're using Linux or MAC, execute `nslookup mongo-1` to see if this DNS is resolvable. Moreover, if your application is running on the host machine, you should use `host` network in the mongo cluster to make it accessible from the application. @robert – Saqib Ahmed Apr 24 '18 at 09:21
  • This answer might help you: https://stackoverflow.com/questions/46743591/initiate-mongodb-replica-set-in-docker-swarm – Saqib Ahmed Apr 24 '18 at 10:18
  • I can solve the problem by adding host file entries for mongo-1, mongo-2 and mongo-3. thx! – robert Apr 24 '18 at 12:42
  • Glad to know you solved it. You won't have to do this if you run this mongo cluster on the same network as your application (containerized of course). You won't have to add anything then. DNS is resolvable from within the containers with the same network. – Saqib Ahmed Apr 24 '18 at 12:57
  • Yes. Thank you! In production this is the case...but in dev I need access from outside :-) – robert Apr 24 '18 at 13:12

0 Answers0