Sign inSign up

markmester/mongo

By markmester

•Updated almost 10 years ago

Docker Image for MongoDB

Image
0

187

markmester/mongo repository overview

⁠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
  1. Start container:
docker run -p 30001:27017 --name mongo1 --net mongo-cluster \
markmester/mongo:latest mongod --replSet mongo-set
  1. 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
  1. Connect to mongo shell in any container:
docker exec -it mongo1 mongo
  1. In the mongo shell, create the db:
> db = (new Mongo('localhost:27017')).getDB('test')
test
  1. 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"
  		},
  	]
}
  1. 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" }
  1. 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

Tag summary

Content type

Image

Digest

Size

-

Last updated

almost 10 years ago

docker pull markmester/mongo