Docker compose for Replica Set
Files structure
- List all files in this example.
startdb.shstartdb.ps1rs-init.shdocker-compose.yml
startdb.sh for Linux / Mac
#!/bin/bashdocker-compose up -dsleep 100docker exec mongo1 /scripts/rs-init.sh
startdb.ps1 for using powershell core
docker-compose up -dsleep 100docker exec mongo1 /scripts/rs-init.sh
rs-intl.sh
#!/bin/bashmongo <<EOFvar config = {"_id": "dbrs","version": 1,"members": [{"_id": 1,"host": "mongo1:27017","priority": 3},{"_id": 2,"host": "mongo2:27017","priority": 2},{"_id": 3,"host": "mongo3:27017","priority": 1}]};rs.initiate(config, { force: true });rs.status();EOF
docker-compose.yml
version: '3.8'services:mongo1:image: mongocontainer_name:mongo1restart: alwayslink:- mongo2- mongo3volumes:- ./data1:/data/db- ./rs-init.sh:/scripts/rs-init.shports:- 27021:27017networks:- mongo-networkentrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "dbrs" ]mongo2:image: mongocontainer_name:mongo1restart: alwaysvolumes:- ./data2:/data/dbports:- 27022:27017networks:- mongo-networkentrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "dbrs" ]mongo3:image: mongocontainer_name:mongo1restart: alwaysvolumes:- ./data3:/data/dbports:- 27023:27017networks:- mongo-networkentrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "dbrs" ]networks:mongors-network:driver: bridge
Useful resources
Loading comments...