A Dockerfile and config for launching a MongoDB contatiner with 3 mongo processes that act as a ReplicaSet. There are config files for each mongo instance that are copied to the container. The shell script handles creation of the data directories and starting mongo.
It's probably not a good idea to run this setup in production as each mongo instance should be split across different machines. However for a local development environment this fits our needs.
- Install Docker.
docker build -t="inlight/mongodb-replica-set" github.com/inlight-media/docker-mongodb-replica-set
Refer to Dockerfile MongoDB for usage notes not specific to ReplicaSet
docker run -itd -p 27017:27017 -p 27018:27018 -p 27019:27019 --name mongodb inlight/mongodb-replica-set
Once the container is running you can initialize the ReplicaSet with the following steps:
- Access shell of container with
docker exec -it mongodb bash
- Access mongo shell with
mongo
rs.initiate()
to initiate ReplicaSet- Use the output of previous step to grab the 'me' (hostname of the machine) property to add to ReplicaSet:
- port 27018:
rs.add('9488f884u84:27018')
- port 27019:
rs.add('9488f884u84:27019')
- port 27018:
- You can use
rs.status()
to see if your ReplicaSet has started successfully