Skip to content

Instantly share code, notes, and snippets.

@dhavaln
Created January 19, 2023 11:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dhavaln/c85e0aa32087b4395f618e09273adbd0 to your computer and use it in GitHub Desktop.
Save dhavaln/c85e0aa32087b4395f618e09273adbd0 to your computer and use it in GitHub Desktop.
Mongodb Replicaset for Swarm
services:
# first, we define the three mongo servers that will act as replicas
# here, we steup the hostname ports, and startup command
# which is the same as discussed in the previous section
mongo1:
hostname: mongo1
image: mongo
expose:
- 27017
ports:
- 30001:27017
restart: always
command: mongod --replSet my-mongo-set
mongo2:
hostname: mongo2
image: mongo
expose:
- 27017
ports:
- 30002:27017
restart: always
command: mongod --replSet my-mongo-set
mongo3:
hostname: mongo3
image: mongo
expose:
- 27017
ports:
- 30003:27017
restart: always
command: mongod --replSet my-mongo-set
# finally, we can define the initialization server
# this runs the `rs.initiate` command to intialize
# the replica set and connect the three servers to each other
mongoinit:
image: mongo
# this container will exit after executing the command
restart: "no"
depends_on:
- mongo1
- mongo2
- mongo3
command: >
mongo --host mongo1:27017 --eval
'
db = (new Mongo("localhost:27017")).getDB("test");
config = {
"_id" : "my-mongo-set",
"members" : [
{
"_id" : 0,
"host" : "mongo1:27017"
},
{
"_id" : 1,
"host" : "mongo2:27017"
},
{
"_id" : 2,
"host" : "mongo3:27017"
}
]
};
rs.initiate(config);
'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment