title: Initiate MongoDB replica set in docker swarm
body: <p>I have created two <b>config servers</b> with replica set name <b>rs0</b> in <b>docker-compose.yml</b> file deployed as <b>sudo docker stack deploy --with-registry-auth --compose-file docker-compose.yml test</b>.</p>
  
  <pre><code>version: "3.3"
  services:
      cfg1-r1:
          image: mongo:3.2
          deploy:
              placement:
                  constraints: [node.hostname == ram-ThinkPad-E470]
              restart_policy:
                  condition: on-failure
          volumes:
              - /opt/mongoshard/tmp:/var/lib/mongodb
              - /opt/mongoshard/mongod.conf:/etc/mongod.conf
          networks:
              - mongoshard  
          command: ["mongod", "--configsvr", "--replSet", "rs0", "--dbpath", "/var/lib/mongodb", "--port", "27017"]
  
      cfg2-r1:
          image: mongo:3.2
          deploy:
              placement:
                  constraints: [node.hostname == prasanna-ThinkPad-E470]
              restart_policy:
                  condition: on-failure
          volumes:
              - /opt/mongoshard/tmp:/var/lib/mongodb
              - /opt/mongoshard/mongod.conf:/etc/mongod.conf
          networks:
              - mongoshard
          command: ["mongod", "--configsvr", "--replSet", "rs0", "--dbpath", "/var/lib/mongodb", "--port", "27017"]
  networks:
      mongoshard:
  </code></pre>
  
  <p>Content in <b>mongod.conf</b> file</p>
  
  <pre><code>storage:
    dbPath: /var/lib/mongodb
    journal:
      enabled: true
  
  systemLog:
    destination: file
    logAppend: true
    path: /var/log/mongodb/mongod.log
  net:
    port: 27017
  </code></pre>
  
  <p>When I try to initiate using service name as</p>
  
  <p><b>sudo docker exec -it $(sudo docker ps -qf label=com.docker.swarm.service.name=test_cfg1-r1) mongo --eval 'rs.initiate({ _id: "rs0", members: [{ _id: 1, host: "cfg1-r1:27017" }, { _id: 2, host: "cfg2-r1:27017" }], settings: { getLastErrorDefaults: { w: "majority", wtimeout: 30000 }}})'</b></p>
  
  <p><b>Edit</b> I Forgot to mention cfg2-r1 while initializing. Now I added it.</p>
  
  <p>It's giving error </p>
  
  <pre><code>MongoDB shell version v3.4.9
  connecting to: mongodb://127.0.0.1:27017
  MongoDB server version: 3.4.9
  {
          "ok" : 0,
          "errmsg" : "No host described in new configuration 1 for replica set rs0 maps to this node",
          "code" : 93,
          "codeName" : "InvalidReplicaSetConfig"
  }
  </code></pre>
  
  <p><b>EDIT:</b> I think the problem is cfg1-r1 and cfg2-r2 services are not in same network. When I deploy stack file, a network called mongo_mongoshard(overlay) is created and when I inspect it, it is showing only cfg1-r1 as containers in it's container list.</p>