Setup MongoDB community server as a single node.
$ docker pull mongodb/mongodb-community-server
Verify MongoDB image is downloaded successfully:
$ docker images
Run docker image
$ docker run --name mongo -p 27017:27017 -d mongodb/mongodb-community-server:latest
Setup MongoDB 3 node cluster on docker
The steps to create a docker cluster are as follows.
- Create a Docker network.
$ docker network create mongoCluster
- Start three instances of MongoDB.
docker run -d --rm -p 27017:27017 --name mongo1 --network mongoCluster mongo mongod --replSet myReplicaSet --bind_ip localhost,mongo1
docker run -d --rm -p 27018:27017 --name mongo2 --network mongoCluster mongo mongod --replSet myReplicaSet --bind_ip localhost,mongo2
docker run -d --rm -p 27019:27017 --name mongo3 --network mongoCluster mongo mongod --replSet myReplicaSet --bind_ip localhost,mongo3
- Initiate the Replica Set.
docker exec -it mongo1 mongosh --eval "rs.initiate({
_id: \"myReplicaSet\",
members: [
{_id: 0, host: \"mongo1\"},
{_id: 1, host: \"mongo2\"},
{_id: 2, host: \"mongo3\"}
]
})"
docker exec -it mongo1 mongosh --eval "rs.status()"
Detailed information on Replica set configuration can be found here.
Once you have a MongoDB cluster up and running, you will be able to experiment with it.
Ref: https://www.mongodb.com/compatibility/deploying-a-mongodb-cluster-with-docker
Using Docker Compose -
docker-compose -f config/docker-compose.yaml up -d
More details on cluster setup can be learned from Mongodb University here.
No comments:
Post a Comment