Initializing MongoDB replica set through a swarm service

Let's try to define a better and easier way to set up a MongoDB replica set.

We'll start by creating three mongo services. Later on, each will become a member of a Mongo replica set:

for i in 1 2 3; do    docker service create --name go-demo-db-rs$i \        --reserve-memory 100m \        --network go-demo \        mongo:3.2.10 mongod --replSet "rs0"    MEMBERS="$MEMBERS go-demo-db-rs$i"done

The only difference, when compared with the previous command we used to create mongo services, is the addition of the environment variable MEMBERS. It holds service names of all MongoDBs. We'll use that as the argument for the next service.

Since the official mongo image does not have a mechanism to configure Mongo replica ...

Get The DevOps 2.1 Toolkit: Docker Swarm now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.