1. Host and container planning
Docker host |
Container name |
Port |
Replica set |
192.168.0.40 |
M40 |
27017 |
Rs0 |
192.168.0.41 |
M41 |
27017 |
Rs0 |
192.168.0.42 |
M42 |
27017 |
Rs0 |
2. Create container 2.1 to create a container on host 40 M40
docker run --name m40 -d -p 27017:27017 -v /etc/localtime:/etc/localtime:ro -v /data/db:/data/db -v /data/configdb:/data/configdb reg.citms.cn/public/mongo:3.2.19 mongod --replSet rs0
2.2. Create a container on host 41 M41
docker run --name m41 -d -p 27017:27017 -v /etc/localtime:/etc/localtime:ro -v /data/db:/data/db -v /data/configdb:/data/configdb reg.citms.cn/public/mongo:3.2.19 mongod --replSet rs0
2.3. Create a container on host 42 M42
docker run --name m42 -d -p 27017:27017 -v /etc/localtime:/etc/localtime:ro -v /data/db:/data/db -v /data/configdb:/data/configdb reg.citms.cn/public/mongo:3.2.19 mongod --replSet rs0
3. Build a replica set
# # #3.1. Connect one of the containers and run the MONGO shell
docker exec -it m40 mongo
# # #3.2. Initialize replica set
rs.initiate( { _id : "rs0", members: [ { _id: 0, host: "192.168.0.40:27017" }, { _id: 1, host: "192.168.0.41:27017" }, { _id: 2, host: "192.168.0.42:27017" } ]})
# # #3.3. View replica set configuration, output slightly
rs.conf()
# # #3.5. Identify the primary node of the replica set, the output is slightly
rs.status()
MongoDB Replica set version-docker