Install MongoDB with Replication on Docker

One of my previous works was to set up an infrastructure with MongoDB. If you asked me why MongoDB, I would say it was due to sort of legacy. The thing was, I had been struggling for replication installation until I found a way to complete. In this post, I share my steps to achieve with some references.

Foremost, a replica set means there are several database nodes maintaining the same data set. In reality, my application will connect to these nodes, Mongo itself will allow writing onto its primary node while secondary will clone data after a short particular period time. When the primary has a problem, the cluster will vote to differentiate new primary and secondaries.

My problem was the incorrect configuration for my cluster, therefore, these nodes can not communicate. My configuration used IP information which will probably cause some communication problems when these addresses change. Anyway, my post just shows how it works. Bear in mind that I use Docker to install MongoDB so need to install Docker first.


Install mongo nodes 1

The default port of mongo is 27017, I installed 3 nodes on 1 machine (suppose my real IP is so I have to open 3 ports for mapping to each mongo container, otherwise I will meet further issues. You can start from 27017 but I choose from 27018. Three containers are mongo1, mongo2 and mongo3 respectively. Replica Set name is rs0.

sudo docker run \
-p 27018:27017 \
--name mongo1 \
--net curator-cluster \
mongo mongod --replSet rs0
sudo docker run  -it\
-p 27019:27017 \
--name mongo2 \
--net curator-cluster \
mongo mongod --replSet rs0
sudo docker run \
-p 27020:27017 \
--name mongo3 \
--net curator-cluster \
mongo mongod --replSet rs0

My configuration

If you make an incorrect configuration, these nodes can not find each other 2 or you can get MongoDB connection error 3.

config = {
  "_id" : "rs0",
  "members" : [
    {"_id" : 0, "host" : ""},
    {"_id" : 1, "host" : ""},
    {"_id" : 2, "host" : ""}


To verify, you can use this command

mongo --host replicaSetName/host1[:porthost1],host2[:porthost1],host3[:porthost3] databaseToConnect

for instance:

mongo --host rs0/,, schedulerinterface-staging


Hello World!

Quy’s Notepad++ is my playground. In here, I would like to share interested stuffs. This blog is underconstruction. I will update very soon.

My blog is hosted by GitHub, uses dbyll theme, you can find theme source in GitHub on Grab your copy now!

updated 10/10/2015 3:34:51 PM My page can use \( \LaTeX \)

updated 10/11/2015 6:26:31 PM My page can highlight code

# Python
import pandas as pd
print "Hello, i'm a message"
/* C/C++ */
#include <iostream>

namespace __hidden__ {
  struct print {
    bool space;
    print() : space(false) {}
    ~print() { std::cout << std::endl; }

    template <typename T>
    print &operator , (const T &t) {
      if (space) std::cout << ' ';
      else space = true;
      std::cout << t;
      return *this;

#define print __hidden__::print(),

int main() {
  int a = 1, b = 2;
  print "this is a test";
  print "the sum of", a, "and", b, "is", a + b;
  return 0;