generated from ztimson/template
Added replica init
All checks were successful
Build and publish / Build Container (push) Successful in 1m47s
All checks were successful
Build and publish / Build Container (push) Successful in 1m47s
This commit is contained in:
@@ -15,5 +15,48 @@ if [ -f /data/db/mongod.lock ] || [ -f /data/db/WiredTiger.lock ] || [ -f /data/
|
||||
echo "Repair complete, starting normally..."
|
||||
fi
|
||||
|
||||
if [ -n "$REPLICA" ]; then
|
||||
# Start mongo temporarily in background with replSet enabled
|
||||
mongod --replSet rs0 --bind_ip_all &
|
||||
MONGO_PID=$!
|
||||
|
||||
# Wait for mongo to be ready
|
||||
echo "Waiting for MongoDB to be ready..."
|
||||
until mongosh --quiet --eval "db.runCommand({ ping: 1 })" &>/dev/null; do
|
||||
sleep 1
|
||||
done
|
||||
|
||||
# Build members array from REPLICA CSV
|
||||
MEMBERS=""
|
||||
INDEX=0
|
||||
IFS=',' read -ra HOSTS <<< "$REPLICA"
|
||||
for HOST in "${HOSTS[@]}"; do
|
||||
[ $INDEX -gt 0 ] && MEMBERS+=","
|
||||
MEMBERS+="{ _id: $INDEX, host: '$HOST' }"
|
||||
INDEX=$((INDEX + 1))
|
||||
done
|
||||
|
||||
# Init or reconfig replica set
|
||||
mongosh --quiet --eval "
|
||||
try {
|
||||
const config = rs.conf();
|
||||
config.members = [$MEMBERS];
|
||||
rs.reconfig(config, { force: true });
|
||||
print('Replica set reconfigured');
|
||||
} catch(e) {
|
||||
rs.initiate({ _id: 'rs0', members: [$MEMBERS] });
|
||||
print('Replica set initiated');
|
||||
}
|
||||
"
|
||||
|
||||
# Stop the temporary mongod
|
||||
kill $MONGO_PID
|
||||
wait $MONGO_PID 2>/dev/null
|
||||
fi
|
||||
|
||||
if [ -n "$REPLICA" ]; then
|
||||
set -- mongod --replSet rs0 --bind_ip_all
|
||||
fi
|
||||
|
||||
# Hand off to normal mongo entrypoint
|
||||
exec /usr/local/bin/docker-entrypoint.sh "$@"
|
||||
exec /usr/local/bin/docker-entrypoint.sh "$@"
|
||||
|
||||
Reference in New Issue
Block a user