Please Note: This article is part of our historical archive. Because it was published a while ago, some of the information, links, or context may now be outdated.
Wonder how to deploy replica set mongodb docker? Our experts have put together this guide to help you get started. Our Docker Support team is here to lend a hand with your queries and issues.
Deploy replica set mongodb docker
Basically, we are going to have three containers, which are all inside their own Docker container network.
Let’s name them
mongo1, mongo2, and mongo3. These are three Mongo instances of our replica set. We are also going to expose each of them to our local machine, so that we can access them using the Mongo shell interface from our local machine.
Each of the three Mongo containers should be able to communicate with all other containers in the network.Advertisements
Creating a New Network
To see all networks currently on your system, run the command
$ docker network ls NETWORK ID NAME DRIVER SCOPE 2a4e341c6039 bridge bridge local 4fbef5286425 host host local 8062e4e7cdca none null localWe will be adding a new network called
:my-mongo-cluster$ docker network create my-mongo-clusterThe new network should now be added to your list of networks :
$ docker network ls NETWORK ID NAME DRIVER SCOPE 2a4e341c6039 bridge bridge local 4fbef5286425 host host local f65e93c94e42 mongo-cluster bridge local 8062e4e7cdca none null localSetting up Our Containers
To start up our first container,
run the command:mongo1$ docker run \ -p 30001:27017 \ --name mongo1 \ --net my-mongo-cluster \ mongo mongod --replSet my-mongo-setLet’s see what each part of this command does :
: Start a container from an imageAdvertisementsdocker run
: Expose port 27017 in our container, as port 30001 on the localhost-p 30001:27017
: name this container “mongo1”--name mongo1
: Add this container to the “my-mongo-cluster” network.--net my-mongo-cluster
: the name of the image we are using to spawn this containermongo
: Run mongod while adding this mongod instance to the replica set named “my-mongo-set”mongod --replSet my-mongo-set
Set up the other 2 containers by running :
$ docker run \ -p 30002:27017 \ --name mongo2 \ --net my-mongo-cluster \ mongo mongod --replSet my-mongo-set $ docker run \ -p 30003:27017 \ --name mongo3 \ --net my-mongo-cluster \ mongo mongod --replSet my-mongo-setConfiguring Database Replication
Now that we have all our Mongo instances up and running, let’s add them to a replica set.
Connect to the Mongo shell in any of the containers.
docker exec -it mongo1 mongoThis command will open up the Mongo shell in our running
container (but you can also run it from themongo1ormongo2container as well).mongo3Inside the Mongo shell, we first create our configuration :
MongoDB shell version v5.0.8 > db = (new Mongo('localhost:27017')).getDB('test') test > config = { "_id" : "my-mongo-set", "members" : [ { "_id" : 0, "host" : "mongo1:27017" }, { "_id" : 1, "host" : "mongo2:27017" }, { "_id" : 2, "host" : "mongo3:27017" } ] }The first
key in the config, should be the same as the_idflag which was set for our mongod instances, which is--replSetin our case. We then list all themy-mongo-setwe want in our replica set.membersSince we added all our Mongo instances to our docker network. Their name in each container resolver to their respective IP addresses in the
network.my-mongo-clusterFinally, start the replica set by running the following command in the Mongo shell:
> rs.initiate(config) { "ok" : 1 }If the command succeeds, your prompt should change to reflect that the current database is part of a replice set:
my-mongo-set:PRIMARY>This means that the shell is currently associated with the
database in ourPRIMARYcluster.my-mongo-setLet’s play around with our new replica set to make sure it works as intended. (I am omitting the
prompt for readability)my-mongo-set:PRIMARY>First, let’s insert a document into our primary database :
> db.mycollection.insert({name : 'sample'}) WriteResult({ "nInserted" : 1 }) > db.mycollection.find() { "_id" : ObjectId("57761827767433de37ff95ee"), "name" : "sample" }We then make a new connection to one of our secondary databases (located on
) and check if our document has replicate:mongo2> db2 = (new Mongo('mongo2:27017')).getDB('test') test > db2.setSecondaryOk() > db2.mycollection.find() { "_id" : ObjectId("57761827767433de37ff95ee"), "name" : "sample" }We run the
command to let the shell know that we are intentionally querying a database that is not our primary.db2.setSecondaryOk()Conclusion
To conclude, our Support Engineers demonstrated how to deploy replica set mongodb docker