Using Docker to Deploy a MongoDB Cluster/Replica Set

Hi everyone! I’m new to kubernetes and as I was exploring what I can do with it I end up with this link here Deploying A MongoDB Cluster With Docker | MongoDB. I created this docker-compose.yml derived from the link, but I’m not sure if I’ve done it correctly.



containers are running just fine and I configured the replica set according to the blog post that I’m following. Everything seems okay, but I’m not sure why on the blog post, volumes was not mentioned at all, anyway as you can see at my docker-compose I added volumes. I tried connecting to port 27017 using mongodb compass and it works, I imported sample data and it seems okay. Now, I tried connecting to port 27018, I got in but I have no access to any database. Is that normal? and also I stopped member1 the default PRIMARY node, I check the rs.status() and member2 became the PRIMARY node. How do I connect to the cluster and automatically connect to the primary node? My experience with Mongodb atlas is not the same and I’m curious how they did :sweat_smile:

I read this https://www.mongodb.com/docs/manual/core/replica-set-members/ that solves one of my question :grin:
My major problem now is connecting into the primary, like what if port 27018 restarts? A secondary node will be assign as the new primary node. So how do I always connect into the primary node without thinking what port I should connect into?