how to create replica set in mongodb in docker


 description

Start mongo replication set in docker, set mount, configuration verification and memory limit

 command

docker run -d --name node-p -p 27027:27017 \
    -v /home/docker/node_p/db:/data/db \
    -v /home/docker/node_p/mongodb-keyfile:/opt/mongodb-keyfile \
    -v /etc/localtime:/etc/localtimmke:ro  \
    --privileged=true \
    --restart=always \
    -e MONGO_INITDB_ROOT_USERNAME=root \
    -e MONGO_INITDB_ROOT_PASSWORD=mfweah0rGxyOH49A \
    mongo:4.2.6 --auth --keyFile /opt/mongodb-keyfile --oplogSize 102400 --wiredTigerCacheSizeGB 16 --replSet "cmdhelp" 

 parameter

run: run the command in the new container
-d: Run the container in the background and print the container ID
--name: assign a name to the container
-p: Publish the port of the container to the host
-v: bind mount volume
--privileged: grant extended privileges to this container
--restart: restart policy when the container exits (default is "No")
-e: set environment variables
--auth: turn on authentication
--keyFile: keyfile path
--oplogSize: It is a special, capped collection. This record holds a rolling record of all operations that modify the data stored in the database. (Default: %5)
--wiredTigerCacheSizeGB: Specify the maximum memory usage (the default calculation rule is: 0.5*(memory size-1G), if it is less than 256, the default is: 256)
--replSet: replication set name

 result

8838249ca398086c...

I hope to help every developer quickly find the commands they need. Of course, you can also add your own commonly used commands for easy searching.