Public Repository

Last pushed: a year ago
Short Description
Docker Image for MongoDB
Full Description
Docker Image for MongoDB: a NoSQL document-oriented database
Run via:
docker run -p 27017:27017 --name mongo_instance_001 -d markmester/mongo:latest
To create a replica set:
  1. Create a network:
    docker network create mongo-cluster
    
  2. Start container:
    docker run -p 30001:27017 --name mongo1 --net mongo-cluster \
    markmester/mongo:latest mongod --replSet mongo-set
    
  3. Exit and start the container:
    docker start <container id>
    
    **get container id via 'docker ps -a'
  4. Repeat steps 2 & 3 for any additional nodes:
    docker run -p 30002:27017 --name mongo2 --net mongo-cluster \
    markmester/mongo:latest mongod --replSet mongo-set
    docker run -p 30003:27017 --name mongo3 --net mongo-cluster \
    markmester/mongo:latest mongod --replSet mongo-set
    
  5. Connect to mongo shell in any container:
    docker exec -it mongo1 mongo
    
  6. In the mongo shell, create the db:
    > db = (new Mongo('localhost:27017')).getDB('test')
    test
    
  7. Create the mongo configuration, making sure thefirst _id key is the same as the --replSet flag set for our mongod instances:
    > config = {
       "_id" : "mongo-set",
       "members" : [
           {
               "_id" : 0,
               "host" : "mongo1:27017"
           },
           {
               "_id" : 1,
               "host" : "mongo2:27017"
           },
       ]
    }
    
  8. Initiate the replicate set:
    rs.initiate(config)
    
To test replication:
  1. Insert a doc into the primary node:
    > db.testcollection.insert({name : 'test'})
    WriteResult({ "nInserted" : 1 })
    > db.testcollection.find()
    { "_id" : ObjectId("57761827767433de37ff95ee"), "name" : "test" }
    
  2. Mkae a connection to the secondary node and allowing querying on a non-primary node:
    > db2 = (new Mongo('mongo2:27017')).getDB('test')
    test
    > db2.setSlaveOk()
    > db2.testcollection.find()
    { "_id" : ObjectId("57761827767433de37ff95ee"), "name" : "test" }
    
Additional Notes:
  • For persistent data storage acccross container shutdown, use Docker volumes and set the appropriate --dbpath when running mongod
Docker Pull Command
Owner
markmester