mongodbdockerdocker-compose

How to do a MongoDB 6 single node replicaset with Docker compose?


On my local Mac M1 PRO, I use for several month now a Docker compose to mount a single node replicaset based on MongoDB 5.

version: "3.9"
services:
  mongodb:
    image: mongo:5
    command: --replSet rs0
    ports:
      - '28017:27017'
    healthcheck:
      test: echo 'db.runCommand("ping").ok' | mongo localhost:27017/admin --quiet
      interval: 2s
      timeout: 3s
      retries: 5

  mongo-init:
    image: mongo:5
    restart: "no"
    depends_on:
      mongodb:
        condition: service_healthy
    command: >
      mongo --host mongodb:27017 --eval
      '
      rs.initiate( {
        _id : "rs0",
        members: [
          { _id: 0, host: "localhost:27017" }
        ]
      })
      '

It works well and I have a simple MongoDB 5 replicaset. Now, I want the same thing with MongoDB 6. So, I modify the image from mongodb:5 to mongodb:6 but the replicaset didn't mount.

I have this error:

{"t":{"$date":"2023-04-14T08:52:52.326+00:00"},"s":"I",  "c":"-", "id":4939300, "ctx":"monitoring-keys-for-HMAC","msg":"Failed to refresh key cache","attr":{"error":"NotYetInitialized: Cannot use non-local read concern until replica set is finished initializing.","nextWakeupMillis":24600}}

I don't need TLS or encryption fancy feature.

What is wrong with my configuration?


Solution

  • The mongo6 docker container has changed some internals, here is a working version for me Port differences arent relevant except for setting your correct ones in the commands/healthchecks.

      mongo:
        image: mongo:6
        command: [--replSet, my-replica-set, --bind_ip_all, --port, "30001"]
        ports:
          - 30001:30001
        healthcheck:
          test: test $$(mongosh --port 30001 --quiet --eval "try {rs.initiate({_id:'my-replica-set',members:[{_id:0,host:\"mongo:30001\"}]})} catch(e) {rs.status().ok}") -eq 1
          interval: 10s
          start_period: 30s