chore: initiate replica with defined host (#62477)

This commit is contained in:
Shaun Hamilton
2025-10-07 11:43:27 +02:00
committed by GitHub
parent c8aeac5ce8
commit 806be69c88
2 changed files with 11 additions and 2 deletions
+3 -1
View File
@@ -13,6 +13,7 @@ services:
image: mongo image: mongo
command: mongod --replSet rs0 command: mongod --replSet rs0
restart: unless-stopped restart: unless-stopped
hostname: mongodb
volumes: volumes:
- mongodb-data:/data/db - mongodb-data:/data/db
mongo-setup: mongo-setup:
@@ -24,8 +25,9 @@ services:
'bash', 'bash',
'-c', '-c',
# This will try to initiate the replica set, until it succeeds twice (i.e. until the replica set is already initialized) # This will try to initiate the replica set, until it succeeds twice (i.e. until the replica set is already initialized)
'mongosh --host mongo-db:27017 --eval ''try {rs.initiate();} catch (err) { if(err.codeName !== "AlreadyInitialized") throw err };''' 'mongosh --host mongo-db:27017 --eval ''try {var cfg = { _id: "rs0", members: [{ _id: 0, host: "mongodb:27017" }] }; rs.initiate(cfg); } catch (err) { if(err.codeName !== "AlreadyInitialized") throw err };'''
] ]
volumes: volumes:
mongodb-data: mongodb-data:
driver: local
+8 -1
View File
@@ -4,6 +4,7 @@ services:
container_name: mongodb container_name: mongodb
command: mongod --replSet rs0 command: mongod --replSet rs0
restart: unless-stopped restart: unless-stopped
hostname: mongodb
ports: ports:
- 27017:27017 - 27017:27017
volumes: volumes:
@@ -22,8 +23,14 @@ services:
# This will try to initiate the replica set, until it succeeds twice (i.e. until the replica set is already initialized) # This will try to initiate the replica set, until it succeeds twice (i.e. until the replica set is already initialized)
command: > command: >
mongosh --host db:27017 --eval ' mongosh --host db:27017 --eval '
var cfg = {
_id: "rs0",
members: [
{ _id: 0, host: "mongodb:27017" }
]
};
try { try {
rs.initiate(); rs.initiate(cfg);
} catch (err) { } catch (err) {
if(err.codeName !== "AlreadyInitialized") throw err; if(err.codeName !== "AlreadyInitialized") throw err;
} }