Docker – How To: Dockerized MongoDB Replication on Elastic Container Service

amazon-ecsamazon-web-servicesdockermongodb

I have a Node.js app together with a MongoDB database running on AWS ECS. The setup I've got currently is formed of 2 ELB's and 2 Instances running each a Docker container with its assigned service (node/mongo):

Elastic Load Balancer -> Dockerized Node -> Elastic Load Balancer -> Dockerized MongoDB

I am running the Official Docker image for MongoDB available here: https://registry.hub.docker.com/_/mongo/

Any pointers as to how would I be able to deploy a replicaSet for MongoDB using Docker and ECS, each replica running on a different instance?

Best Answer

Don't dockerize databases. Docker containers are meant to be ephemeral and not hold data. That's not appropriate for databases.

In particular, do not attempt to dockerize a MongoDB cluster. The setup for replica sets is extremely stateful and complex. You have to run many commands in order and only once. If you miss any command or if a node goes off, the entire cluster might dead lock and you'll be forced to start over.

Docker has its own idea of how applications should behave. It would be really difficult to make that work inside Docker.

If you want a cluster with 3 replicas, you should get 3 AWS instances and setup MongoDB on the instances. Search for mongodb setting up a replica set on <OS> on Google, that will give you an up to date tutorial.