# MongoDB Replication Example Using a Docker This [MongoDB replication](https://docs.mongodb.com/manual/replication/) example uses a [Docker engine](http://docker.com) to run replica set members. **This platform is mainly for developing and testing.** ## Getting Started You will need an Docker engine where you can run containers. If you want to avoid running all replset members on one host, you can also use [Docker Swarm](https://docs.docker.com/swarm/). ## Example Working Scenarios This section describes how this example is designed to work. **All practices for [MongoDB replication](https://docs.mongodb.com/manual/replication/) applies also to this example** ### Initial Deployment: 3-member Replica Set To create replica set with three members you can use this script: ```bash cat > variables < ``` Note: You can also use host version of mongo shell, but you have to substitute values of environmental variables and IP address instead of `localhost` by yourself. During the lifetime of your deployment, one or more of those members might crash or fail. It is possible to configure container to get restarted automatically (see [docker run reference](https://docs.docker.com/engine/reference/run/#restart-policies---restart). **Note**: for production usage, you should maintain as much separation between members as possible. It is recommended to run containers on different hosts. ### Adding member To add a new member into replicaset run: ```bash docker run -d --cidfile $CIDFILE_DIR/replset3 --name=replset-3 --hostname=replset-3 --network ${network_name} --network-alias mongodb --env-file=variables $IMAGE_NAME run-mongod-replication docker exec replset-3 bash -c "while ! [ -f /tmp/initialized ]; do sleep 1; done" ``` New container is created and it automatically connects to the replica set. ### Removing member To prevent possible data lost, automatic removing of members from replicaset is not supported. To do it: 1. stop the container (for example `docker stop replset-2`) 2. connect to replica set and [remove the member](https://docs.mongodb.com/manual/tutorial/remove-replica-set-member/) ### Known Limitations * Adding or removing new member to replica set takes some time (elections, syncing,...), so after your command finished it may take some time until replica set is ready