Skip to content

Instantly share code, notes, and snippets.

@crapthings
Last active November 20, 2022 12:31
Show Gist options
  • Select an option

  • Save crapthings/71fb6156a8e9b31a2fa7946ebd7c4edc to your computer and use it in GitHub Desktop.

Select an option

Save crapthings/71fb6156a8e9b31a2fa7946ebd7c4edc to your computer and use it in GitHub Desktop.
docker-compose rs init
version: '3'
services:
mongo1:
hostname: mongo1
container_name: localmongo1
image: mongo
expose:
- 27017
ports:
- 27017:27017
restart: always
entrypoint: ["/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0"]
# volumes:
# - /data/db/mongotest:/data/db # This is where your volume will persist. e.g. VOLUME-DIR = ./volumes/mongodb
mongo2:
hostname: mongo2
container_name: localmongo2
image: mongo
expose:
- 27017
ports:
- 27018:27017
restart: always
entrypoint: ["/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0"]
mongo3:
hostname: mongo3
container_name: localmongo3
image: mongo
expose:
- 27017
ports:
- 27019:27017
restart: always
entrypoint: ["/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0"]
mongo4:
hostname: mongo4
container_name: localmongo4
build:
context: .
dockerfile: rsinit
environment:
WAIT_HOSTS: mongo1:27017, mongo2:27017, mongo3:27017
entrypoint: ["sh", "-c", "rs.sh"]
#!/bin/bash
check_db_status() {
mongo1=$(mongo --host mongo1 --port 27017 --eval "db.stats().ok" | tail -n1 | grep -E '(^|\s)1($|\s)')
mongo2=$(mongo --host mongo2 --port 27017 --eval "db.stats().ok" | tail -n1 | grep -E '(^|\s)1($|\s)')
mongo3=$(mongo --host mongo3 --port 27017 --eval "db.stats().ok" | tail -n1 | grep -E '(^|\s)1($|\s)')
if [[ $mongo1 == 1 ]] && [[ $mongo2 == 1 ]] && [[ $mongo3 == 1 ]]; then
init_rs
else
check_db_status
fi
}
init_rs() {
ret=$(mongo --host mongo1 --port 27017 --eval "rs.initiate({ _id: 'rs0', members: [{ _id: 0, host: 'mongo1:27017' }, { _id: 1, host: 'mongo2:27017' }, { _id: 2, host: 'mongo3:27017' } ] })" > /dev/null 2>&1)
exit 0
}
check_db_status > /dev/null 2>&1
FROM mongo
ADD rs.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/rs.sh
@guillegette
Copy link

Hi there, any ideas how to update rs.sh so we don't use --eval ? it is not supported on mongo 4.2 any more.

@crapthings
Copy link
Author

Hi there, any ideas how to update rs.sh so we don't use --eval ? it is not supported on mongo 4.2 any more.

hey i will check to see if its support newer mongo version

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment