Skip to content

Instantly share code, notes, and snippets.

@crapthings
Last active November 20, 2022 12:31
Show Gist options
  • Save crapthings/71fb6156a8e9b31a2fa7946ebd7c4edc to your computer and use it in GitHub Desktop.
Save crapthings/71fb6156a8e9b31a2fa7946ebd7c4edc to your computer and use it in GitHub Desktop.

Revisions

  1. crapthings revised this gist Dec 11, 2019. 1 changed file with 9 additions and 8 deletions.
    17 changes: 9 additions & 8 deletions docker-compose.yml
    Original file line number Diff line number Diff line change
    @@ -4,36 +4,36 @@ services:
    hostname: mongo1
    container_name: localmongo1
    image: mongo
    restart: always
    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
    command: '--quiet --bind_ip_all --replSet rs0'
    volumes:
    - /data/db/mongo:/data/db

    mongo2:
    hostname: mongo2
    container_name: localmongo2
    image: mongo
    restart: always
    expose:
    - 27017
    ports:
    - 27018:27017
    restart: always
    entrypoint: ["/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0"]
    command: '--quiet --bind_ip_all --replSet rs0'

    mongo3:
    hostname: mongo3
    container_name: localmongo3
    image: mongo
    restart: always
    expose:
    - 27017
    ports:
    - 27019:27017
    restart: always
    entrypoint: ["/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0"]
    command: '--quiet --bind_ip_all --replSet rs0'

    rsinit:
    build:
    @@ -44,3 +44,4 @@ services:
    - mongo2
    - mongo3
    entrypoint: ["sh", "-c", "rs.sh"]

  2. crapthings revised this gist Jul 5, 2019. 2 changed files with 12 additions and 4 deletions.
    4 changes: 4 additions & 0 deletions docker-compose.yml
    Original file line number Diff line number Diff line change
    @@ -39,4 +39,8 @@ services:
    build:
    context: .
    dockerfile: rsinit
    depends_on:
    - mongo1
    - mongo2
    - mongo3
    entrypoint: ["sh", "-c", "rs.sh"]
    12 changes: 8 additions & 4 deletions rs.sh
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,11 @@
    #!/bin/bash

    echo "prepare rs initiating"

    check_db_status() {
    mongo1=$(mongo --host mongo1 --port 27017 --eval "db.stats().ok" | tail -1 | grep -E '(^|\s)1($|\s)')
    mongo2=$(mongo --host mongo2 --port 27017 --eval "db.stats().ok" | tail -1 | grep -E '(^|\s)1($|\s)')
    mongo3=$(mongo --host mongo3 --port 27017 --eval "db.stats().ok" | tail -1 | grep -E '(^|\s)1($|\s)')
    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
    @@ -13,7 +15,9 @@ check_db_status() {

    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

    echo "rs initiating finished"
    exit 0
  3. crapthings revised this gist Jul 5, 2019. 1 changed file with 1 addition and 3 deletions.
    4 changes: 1 addition & 3 deletions docker-compose.yml
    Original file line number Diff line number Diff line change
    @@ -39,6 +39,4 @@ services:
    build:
    context: .
    dockerfile: rsinit
    environment:
    WAIT_HOSTS: mongo1:27017, mongo2:27017, mongo3:27017
    entrypoint: ["sh", "-c", "rs.sh"]
    entrypoint: ["sh", "-c", "rs.sh"]
  4. crapthings revised this gist Jul 5, 2019. 1 changed file with 2 additions and 4 deletions.
    6 changes: 2 additions & 4 deletions docker-compose.yml
    Original file line number Diff line number Diff line change
    @@ -35,12 +35,10 @@ services:
    restart: always
    entrypoint: ["/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0"]

    mongo4:
    hostname: mongo4
    container_name: localmongo4
    rsinit:
    build:
    context: .
    dockerfile: rsinit
    environment:
    WAIT_HOSTS: mongo1:27017, mongo2:27017, mongo3:27017
    entrypoint: ["sh", "-c", "rs.sh"]
    entrypoint: ["sh", "-c", "rs.sh"]
  5. crapthings revised this gist Jul 5, 2019. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions rs.sh
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,9 @@
    #!/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)')
    mongo1=$(mongo --host mongo1 --port 27017 --eval "db.stats().ok" | tail -1 | grep -E '(^|\s)1($|\s)')
    mongo2=$(mongo --host mongo2 --port 27017 --eval "db.stats().ok" | tail -1 | grep -E '(^|\s)1($|\s)')
    mongo3=$(mongo --host mongo3 --port 27017 --eval "db.stats().ok" | tail -1 | grep -E '(^|\s)1($|\s)')
    if [[ $mongo1 == 1 ]] && [[ $mongo2 == 1 ]] && [[ $mongo3 == 1 ]]; then
    init_rs
    else
  6. crapthings created this gist Jul 5, 2019.
    46 changes: 46 additions & 0 deletions docker-compose.yml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    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"]
    19 changes: 19 additions & 0 deletions rs.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    #!/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
    5 changes: 5 additions & 0 deletions rsinit
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    FROM mongo

    ADD rs.sh /usr/local/bin/

    RUN chmod +x /usr/local/bin/rs.sh