Local replica set on docker-compose

Hey,

I am using docker-compose with a couple of container to create a local replica set.
As soon as I moved to Mongodb5 the replica set started breaking.

my docker-compose file looks like this:

version: "3"
services:
  mongo1:
    hostname: mongo1
    image: mongo
    expose:
      - 27017
    ports:
      - 27017:27017
    restart: always
    command: --replSet rs0 --bind_ip_all
    volumes:
      - ../DB/localMongoData/db:/data/db
    healthcheck:
      test: test $$(echo "rs.initiate().ok || rs.status().ok" | mongo -u $${MONGO_INITDB_ROOT_USERNAME} -p $${MONGO_INITDB_ROOT_PASSWORD} --quiet) -eq 1
      interval: 10s
    # logging:
    #   driver: none

  mongo2:
    hostname: mongo2
    image: mongo
    expose:
      - 27017
    ports:
      - 27018:27017
    restart: always
    command: --replSet rs0 --bind_ip_all
    # logging:
    #   driver: none

  mongo3:
    hostname: mongo3
    image: mongo
    expose:
      - 27017
    ports:
      - 27019:27017
    restart: always
    command: --replSet rs0 --bind_ip_all
    # logging:
    #   driver: none

inside the 1st container I ran

rs.initiate(
  {
    _id : 'rs0',
    members: [
      { _id : 0, host : "mongo1:27017" },
      { _id : 1, host : "mongo2:27017" },
      { _id : 2, host : "mongo3:27017", arbiterOnly: true }
    ]
  }
)

to create the replica set and it worked on mongo 4.x.x

I have tried searching for a solution and I couldn’t find anything that really works, I need a replica set for transactions.
any suggestions and ideas are welcome, I also tried using one container with mongo 5 and standalone replica set but I couldn’t connect to it

1 Like

Same problem here. :frowning:

Something that has worked for me was to clear the volumes of the mongo cluster and reconfigure the replica set.
I had stale configuration there and that is why I had issue.

Bump - having similar problems.