-
-
Save abdelfattahradwan/b5f9760d72f6badc70169cb2581fbb9c to your computer and use it in GitHub Desktop.
A Docker Compose file that spins up a MongoDB replica set with one master and two slave nodes.
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
MONGODB_ROOT_USERNAME="YOUR_USERNAME_HERE" | |
MONGODB_ROOT_PASSWORD="YOUR_PASSWORD_HERE" | |
# Use `openssl rand -base64 756` to generate a valid MongoDB key. | |
MONGO_REPLICA_SET_KEY="YOUR_REPLICA_SET_KEY_HERE" |
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
name: mongodb_replica_set | |
services: | |
master: | |
image: mongo:latest | |
command: mongod --port 27017 --replSet rs0 --bind_ip_all --keyFile /data/configdb/keyfile | |
ports: | |
- "27017:27017" | |
extra_hosts: | |
- "host.docker.internal:host-gateway" | |
restart: unless-stopped | |
environment: | |
- MONGO_INITDB_ROOT_USERNAME=${MONGODB_ROOT_USERNAME} | |
- MONGO_INITDB_ROOT_PASSWORD=${MONGODB_ROOT_PASSWORD} | |
- MONGO_REPLICA_SET_KEY=${MONGO_REPLICA_SET_KEY} | |
volumes: | |
- ./keyfile.sh:/docker-entrypoint-initdb.d/keyfile.sh | |
- master_data_db:/data | |
- master_data_configdb:/data/configdb | |
healthcheck: | |
test: 'mongosh --quiet --port 27017 --eval "db.runCommand({ ping: 1 }).ok" | grep 1' | |
interval: 5s | |
slave_1: | |
image: mongo:latest | |
command: mongod --port 27018 --replSet rs0 --bind_ip_all --keyFile /data/configdb/keyfile | |
ports: | |
- "27018:27018" | |
extra_hosts: | |
- "host.docker.internal:host-gateway" | |
restart: unless-stopped | |
environment: | |
- MONGO_REPLICA_SET_KEY=${MONGO_REPLICA_SET_KEY} | |
volumes: | |
- ./keyfile.sh:/docker-entrypoint-initdb.d/keyfile.sh | |
- slave_1_data_db:/data | |
- slave_1_data_configdb:/data/configdb | |
healthcheck: | |
test: 'mongosh --quiet --port 27018 --eval "db.runCommand({ ping: 1 }).ok" | grep 1' | |
slave_2: | |
image: mongo:latest | |
command: mongod --port 27019 --replSet rs0 --bind_ip_all --keyFile /data/configdb/keyfile | |
ports: | |
- "27019:27019" | |
extra_hosts: | |
- "host.docker.internal:host-gateway" | |
restart: unless-stopped | |
environment: | |
- MONGO_REPLICA_SET_KEY=${MONGO_REPLICA_SET_KEY} | |
volumes: | |
- ./keyfile.sh:/docker-entrypoint-initdb.d/keyfile.sh | |
- slave_2_data_db:/data | |
- slave_2_data_configdb:/data/configdb | |
healthcheck: | |
test: 'mongosh --quiet --port 27019 --eval "db.runCommand({ ping: 1 }).ok" | grep 1' | |
init: | |
image: mongo:latest | |
restart: no | |
extra_hosts: | |
- "host.docker.internal:host-gateway" | |
depends_on: | |
master: | |
condition: service_healthy | |
slave_1: | |
condition: service_healthy | |
slave_2: | |
condition: service_healthy | |
command: > | |
mongosh -u root -p root --host host.docker.internal --port 27017 --eval ' | |
rs.initiate({ | |
_id: "rs0", | |
members: [ | |
{ _id: 0, host: "host.docker.internal:27017" }, | |
{ _id: 1, host: "host.docker.internal:27018" }, | |
{ _id: 2, host: "host.docker.internal:27019" } | |
] | |
}) | |
' | |
volumes: | |
master_data_db: | |
master_data_configdb: | |
slave_1_data_db: | |
slave_1_data_configdb: | |
slave_2_data_db: | |
slave_2_data_configdb: | |
networks: | |
default: | |
name: mongodb_replica_set_network |
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
#!/bin/bash | |
# Echo MONGO_REPLICA_SET_KEY to the keyfile. | |
echo $MONGO_REPLICA_SET_KEY > /data/configdb/keyfile | |
# Change the permissions of the keyfile. | |
chmod 400 /data/configdb/keyfile | |
# Change the ownership of the keyfile. | |
chown mongodb:mongodb /data/configdb/keyfile |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment