Last active
April 25, 2025 15:30
-
-
Save jeromegamez/051a3d8cc07b0f78bb86ef899c352500 to your computer and use it in GitHub Desktop.
Local infra for MongoDB with replicasets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| services: | |
| mongo: | |
| image: mongo:8.0 | |
| container_name: mongo-rs | |
| ports: | |
| - "27017:27017" | |
| command: ["--replSet", "rs0", "--notablescan"] | |
| volumes: | |
| - mongo_data:/data/db | |
| healthcheck: | |
| test: ["CMD", "mongosh", "--quiet", "--eval", "db.runCommand('ping').ok"] | |
| interval: 5s | |
| timeout: 5s | |
| retries: 10 | |
| mongo-init-replica: | |
| image: mongo:8.0 | |
| depends_on: | |
| mongo: | |
| condition: service_healthy | |
| entrypoint: > | |
| sh -c ' | |
| echo "β³ Waiting for Mongo to accept connections..."; | |
| until mongosh --host mongo --quiet --eval "db.runCommand({ ping: 1 }).ok" | grep 1 > /dev/null; do | |
| sleep 1; | |
| done | |
| echo "π Checking if replica set is already initialized..."; | |
| IS_INIT=$(mongosh --host mongo --quiet --eval "try { rs.status().ok } catch(e) { 0 }") | |
| if [ "$$IS_INIT" = "1" ]; then | |
| echo "β Replica set already initialized."; | |
| else | |
| echo "π Initiating replica set..."; | |
| mongosh --host mongo --eval "rs.initiate({ _id: \"rs0\", members: [{ _id: 0, host: \"localhost:27017\" }] })"; | |
| fi | |
| ' | |
| restart: "no" | |
| volumes: | |
| mongo_data: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| services: | |
| mongo: | |
| image: mongo:8.0 | |
| container_name: mongo-rs | |
| ports: | |
| - "27017:27017" | |
| command: ["--replSet", "rs0"] | |
| volumes: | |
| - mongo_data:/data/db | |
| healthcheck: | |
| test: ["CMD", "mongosh", "--quiet", "--eval", "db.runCommand('ping').ok"] | |
| interval: 5s | |
| timeout: 5s | |
| retries: 10 | |
| mongo-init-replica: | |
| image: mongo:8.0 | |
| depends_on: | |
| mongo: | |
| condition: service_healthy | |
| entrypoint: > | |
| sh -c ' | |
| echo "β³ Waiting for Mongo to accept connections..."; | |
| until mongosh --host mongo --quiet --eval "db.runCommand({ ping: 1 }).ok" | grep 1 > /dev/null; do | |
| sleep 1; | |
| done | |
| echo "π Checking if replica set is already initialized..."; | |
| IS_INIT=$(mongosh --host mongo --quiet --eval "try { rs.status().ok } catch(e) { 0 }") | |
| if [ "$$IS_INIT" = "1" ]; then | |
| echo "β Replica set already initialized."; | |
| else | |
| echo "π Initiating replica set..."; | |
| mongosh --host mongo --eval "rs.initiate({ _id: \"rs0\", members: [{ _id: 0, host: \"localhost:27017\" }] })"; | |
| fi | |
| ' | |
| restart: "no" | |
| volumes: | |
| mongo_data: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| SHELL := bash | |
| .ONESHELL: | |
| .SHELLFLAGS := -eu -o pipefail -c | |
| .DELETE_ON_ERROR: | |
| MAKEFLAGS += --warn-undefined-variables | |
| MAKEFLAGS += --no-builtin-rules | |
| .PHONY: mongo-up | |
| mongo-up: ## Spin up MongoDB WITHOUT index checks | |
| docker compose -f compose-mongo.yml up -d | |
| .PHONY: mongo-down | |
| mongo-down: ## Shut down MongoDB | |
| docker compose -f compose-mongo.yml -f compose-mongo-notable.yml down --remove-orphans | |
| .PHONY: mongo-notable-up | |
| mongo-notable-up: ## Spin up MongoDB WITH index checks | |
| docker compose -f compose-mongo-notable.yml up -d | |
| .PHONY: help | |
| help: | |
| @awk 'BEGIN {FS = ":.*##"} /^[a-zA-Z0-9_-]+:.*##/ {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| β― make help | |
| mongo-up Spin up MongoDB WITHOUT index checks | |
| mongo-down Shut down MongoDB | |
| mongo-notable-up Spin up MongoDB WITH index checks | |
| β― task -l | |
| task: Available tasks for this project: | |
| * mongo-down: Shut down MongoDB | |
| * mongo-notable-up: Spin up MongoDB with index checks | |
| * mongo-up: Spin up MongoDB without index checks |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # https://taskfile.dev | |
| version: '3' | |
| vars: | |
| tasks: | |
| mongo-up: | |
| desc: Spin up MongoDB without index checks | |
| preconditions: | |
| - test -f compose-mongo.yml | |
| cmds: | |
| - docker compose -f compose-mongo.yml up -d | |
| silent: false | |
| mongo-notable-up: | |
| desc: Spin up MongoDB with index checks | |
| preconditions: | |
| - test -f compose-mongo-notable.yml | |
| cmds: | |
| - docker compose -f compose-mongo-notable.yml up -d | |
| silent: false | |
| mongo-down: | |
| desc: Shut down MongoDB | |
| preconditions: | |
| - test -f compose-mongo.yml | |
| - test -f compose-mongo-notable.yml | |
| cmds: | |
| - docker compose -f compose-mongo.yml -f compose-mongo-notable.yml down --remove-orphans | |
| silent: false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment