Mongo replica node with docker Mongo replica node with docker mongodb mongodb

Mongo replica node with docker


You could check a guide like "Deploy a MongoDB Cluster in 9 steps Using Docker", but that involve multiple working docker servers.

It uses a key file key file to be used on all nodes (openssl rand -base64 741 > mongodb-keyfile) for internal authentication.
That allows to create an admin user, which then adds the replica servers:

docker run \--name mongo \-v /home/core/mongo-files/data:/data/db \-v /home/core/mongo-files:/opt/keyfile \--hostname="node1.example.com" \--add-host node1.example.com:${node1} \--add-host node2.example.com:${node2} \--add-host node3.example.com:${node3} \-p 27017:27017 -d mongo:2.6.5 \--smallfiles \--keyFile /opt/keyfile/mongodb-keyfile \--replSet "rs0"

On each replica, you initiate and then check the config:

 rs.initiate() rs.conf()

Back to node 1, you declare the replica:

rs0:PRIMARY> rs.add("node2.example.com")rs0:PRIMARY> rs.add("node3.example.com")


There are number of steps to solve this problem with docker:

  1. switch to db admin

use admin

  1. Create a new site admin user
db.createUser( {         user: "siteUserAdmin",         pwd: "password",         roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]       });
  1. Create a new root user

db.createUser( { user: "siteRootAdmin", pwd: "password", roles: [ { role: "root", db: "admin" } ] });

  1. We can now initiate the replica set:
   rs.initiate()    {             "info2" : "no configuration explicitly specified -- making one",             "me" : "node1.example.com:27017",             "info" : "Config now saved locally.  Should come online in about a minute.",             "ok" : 1    }
  1. Verify the initial replica & check the configuration :
rs0:PRIMARY> rs.conf(){        "_id" : "rs0",        "version" : 1,r        "members" : [              {                  "_id" : 0,                  "host" : "node1.example.com:27017"              }        ]}


I built an image which provide env variables for replication and sharded cluster:khezen/mongo

replica set is initialized automatically.