title: How to create mongo replica set on docker swarm global mode
body: <p>I would like to setup 3 member mongo replica set on docker swarm with <code>mode = global</code> and stuck in enabling replication.
  For e.g. Docker swarm nodes: node1, node2, node3 (all manager). All nodes were created on different VM's.</p>
  
  <p>Next created 1 service (mongo1) and executed in global mode to install 1 mongo container/instance on each node (node1, node2, node3) in swarm cluster. </p>
  
  <p>Now, I am facing problem in initializing replica-set without using IP/hostname of host machine as mongo container port is not exposed/mapped to host port.</p>
  
  <p>Command to intialize replica-set from mongo prompt:  </p>
  
  <pre><code>rs.initiate({_id : "rs", members: [{_id: 0, host: "{host1}"},  
              {_id: 1, host: "{host2}"}, {_id:2, host:"{host3}"}]})
  </code></pre>
  
  <p><code>{host1}, {host2}, {host3}</code> are placeholders.</p>
  
  <p>Is there anyway to initialize 3 member replica set without using host IP address?</p>
  
  <p>This can also be setup by using swarm mode = replicated where 3 services has to be created with 1 replica and use service-name to initialize replica like below:  </p>
  
  <pre><code>rs.initiate({_id : "rs", members: [{_id: 0, host: "mongo1"},  
              {_id: 1, host: "mongo2"}, {_id:2, host:"mongo3"}]})
  </code></pre>
  
  <p><code>mono1, mongo2 &amp; mongo3</code> are service name in replicated mode.
  But I would like to do setup in GLOBAL mode only.</p>
  
  <p>Below is the stack file (global mode)</p>
  
  <pre><code>stack-file.yml:
  version: "3.2"
  services:
    mongo1:
      image: mongo:latest
      volumes:
        - mongo_data:/data/db
      networks:
        - mongo_net
      deploy:
        endpoint_mode: dnsrr
        mode: global
      command: mongod --replSet rs
  
  volumes:
    mongo_data:
  
  networks:
    mongo_net:
      external: true
  </code></pre>
  
  <p>To create network
  docker network create --driver overlay mongo_net</p>