Skip to content

Instantly share code, notes, and snippets.

@ro-tex
Last active September 29, 2021 11:22
Show Gist options
  • Save ro-tex/e81726160f98a3acca2cf654fff63177 to your computer and use it in GitHub Desktop.
Save ro-tex/e81726160f98a3acca2cf654fff63177 to your computer and use it in GitHub Desktop.
MongoDB test replica set with a single container
#!/bin/zsh
# You will need a keyfile. This is how you generate one:
# `openssl rand -base64 756 > mgkey`
# It needs to have specific permissions, so `chmod 400 mgkey` it.
# Start the MongoDB container.
docker run \
-d \
--rm \
--detach \
--name mg \
-p 27017:27017 \
-e MONGO_INITDB_ROOT_USERNAME=<root> \
-e MONGO_INITDB_ROOT_PASSWORD=<root password> \
-v <path to keyfile>:/data/mgkey \
-v <path to data dir>:/data/db \
mongo:4.4.1 mongod --port=27017 --replSet=my_repl_set --keyFile=/data/mgkey
# Wait for MongoDB to start.
sleep 3
# Tell MongoDB to initialise a replica set with localhost as the only member.
docker exec mg mongo -u <root> -p <root password> --port 27017 --eval "rs.initiate({_id: \"my_repl_set\", members: [{ _id: 0, host: \"localhost:27017\" }]})"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment