Last active
March 10, 2026 15:36
-
-
Save cheeyeo/4e820fcd96ddd7f87f821c62f9fa9006 to your computer and use it in GitHub Desktop.
Mongodb Replicaset docker compose template
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
| db = db.getSiblingDB(process.env.MONGO_INITDB_DATABASE); | |
| db.createUser({ | |
| user: process.env.MONGO_USER, | |
| pwd: process.env.MONGO_PASSWORD, | |
| roles: [{ | |
| role: 'readWrite', | |
| db: process.env.MONGO_INITDB_DATABASE, | |
| }] | |
| }); | |
| db.createCollection("uploads", {changeStreamPreAndPostImages: { enabled: true }}); |
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
| #!/usr/bin/env bash | |
| # Wait until the mongo-master responds | |
| until mongosh --host mongo1 -u ${MONGO_DB_ROOT_USERNAME} -p ${MONGO_DB_ROOT_PASSWORD} --eval "db.adminCommand('ping')" > /dev/null 2>&1; do | |
| sleep 2 | |
| done | |
| echo "mongo1 is ready. Initiating replica set..." | |
| mongosh --host mongo1 -u ${MONGO_DB_ROOT_USERNAME} -p ${MONGO_DB_ROOT_PASSWORD} --eval "try { rs.status() } catch (err) { rs.initiate({_id:'rs0',members:[{_id:0,host:'mongo1:27017',priority:2},{_id:1,host:'mongo2:27017',priority:1},{_id:2,host:'mongo3:27017',priority:1}]});}" |
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-1: | |
| env_file: .env | |
| container_name: 'mongo1' | |
| entrypoint: > | |
| /bin/bash -c ' | |
| openssl rand -base64 756 > /data/keyfile.key && | |
| chmod 400 /data/keyfile.key && | |
| chown mongodb:mongodb /data/keyfile.key && | |
| /usr/local/bin/docker-entrypoint.sh mongod --replSet rs0 --keyFile /data/keyfile.key --bind_ip localhost,mongo1' | |
| image: 'mongo:latest' | |
| ports: | |
| - 27017:27017 | |
| environment: | |
| - MONGO_INITDB_ROOT_USERNAME=${MONGO_DB_ROOT_USERNAME} | |
| - MONGO_INITDB_ROOT_PASSWORD=${MONGO_DB_ROOT_PASSWORD} | |
| - MONGO_INITDB_DATABASE=fastapiuploads | |
| - MONGO_USER=${MONGO_DB_USER} | |
| - MONGO_PASSWORD=${MONGO_DB_PASSWORD} | |
| volumes: | |
| - 'mongo1data:/data/db' | |
| - 'mongo1config:/data/configdb' | |
| - 'sharedconfig:/data' | |
| - ./scripts/docker/mongo:/docker-entrypoint-initdb.d | |
| healthcheck: | |
| test: mongosh | |
| -u $${MONGO_INITDB_ROOT_USERNAME} | |
| -p $${MONGO_INITDB_ROOT_PASSWORD} | |
| --eval "db.runCommand("ping").ok" | mongosh --port 27017 --quiet | |
| interval: 30s | |
| timeout: 10s | |
| retries: 5 | |
| networks: | |
| - mynetwork | |
| restart: unless-stopped | |
| mongo-2: | |
| container_name: "mongo2" | |
| image: 'mongo:latest' | |
| ports: | |
| - 27018:27017 | |
| volumes: | |
| - 'mongo2data:/data/db' | |
| - 'mongo2config:/data/configdb' | |
| - 'sharedconfig:/data' | |
| command: ["--replSet", "rs0", "--bind_ip", "localhost,mongo2", "--port", "27017", "--keyFile", "/data/keyfile.key"] | |
| networks: | |
| - mynetwork | |
| restart: unless-stopped | |
| mongo-3: | |
| container_name: "mongo3" | |
| image: 'mongo:latest' | |
| ports: | |
| - 27019:27017 | |
| volumes: | |
| - 'mongo3data:/data/db' | |
| - 'mongo3config:/data/configdb' | |
| - 'sharedconfig:/data' | |
| command: ["--replSet", "rs0", "--bind_ip", "localhost,mongo3", "--port", "27017", "--keyFile", "/data/keyfile.key"] | |
| networks: | |
| - mynetwork | |
| restart: unless-stopped | |
| mongo-setup: | |
| image: mongo:latest | |
| env_file: .env | |
| depends_on: | |
| - mongo-1 | |
| - mongo-2 | |
| - mongo-3 | |
| volumes: | |
| - ./mongo-setup.sh:/mongo-setup.sh | |
| entrypoint: ["bash", "/mongo-setup.sh"] | |
| networks: | |
| - mynetwork | |
| mongo-express: | |
| image: mongo-express | |
| env_file: .env | |
| restart: always | |
| ports: | |
| - 8081:8081 | |
| networks: | |
| - "mynetwork" | |
| environment: | |
| ME_CONFIG_MONGODB_URL: mongodb://${MONGO_DB_ROOT_USERNAME}:${MONGO_DB_ROOT_PASSWORD}@mongo1:27017,mongo2:27017,mongo3:27017/?authSource=admin | |
| ME_CONFIG_BASICAUTH_ENABLED: true | |
| ME_CONFIG_BASICAUTH_USERNAME: mongoexpressuser | |
| ME_CONFIG_BASICAUTH_PASSWORD: mongoexpresspass | |
| volumes: | |
| mongo1data: | |
| mongo1config: | |
| mongo2data: | |
| mongo2config: | |
| mongo3data: | |
| mongo3config: | |
| sharedconfig: | |
| networks: | |
| mynetwork: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment