V 6 docker one node replica set

I tried to make a docker container that runs a Mongo DB 6.0.5 as a single-node replication set.
But every time when I tried, the container stopped, and I got an error: MongoServerError: This node was not started with replication enabled.

DockerFile:

FROM mongo:latest
ENV TZ="Europe/Budapest"
ENV TIME_ZONE="Europe/Budapest"
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && dpkg-reconfigure -f noninteractive tzdata
RUN echo "rs.initiate({'_id':'rs0', members: [{'_id':1, 'host':'127.0.0.1:27017'}]});" > "/docker-entrypoint-initdb.d/init_replicaset.js"
RUN echo "********************" > "/tmp/key.file"
COPY ./mongo_init.js /docker-entrypoint-initdb.d/init_users.js
RUN chmod 600 /tmp/key.file
RUN chown 999:999 /tmp/key.file

Dokcer-compose:

version: '3.7'
services:
  mongodb:
    image: mongo6.0.5.t
    container_name: mongo
    hostname: mongo
    environment:
      - TZ=Europe/Budapest
    env_file:
      - ./mongo.env
    ports:
      - 27017:27017
    command: mongod --replSet "rs0" --bind_ip_all --keyFile /tmp/key.file
volumes:
  mongodb_data:

I tried the old one which is good with 4.4 or 5.0:

version: '3.7'
services:
  mongodb:
    image: mongo6.0.5.t
    container_name: mongo
    hostname: mongo
    environment:
      - TZ=Europe/Budapest
    env_file:
      - ./mongo.env
    volumes:
      - mongodb_data:/data/db
    ports:
      - 27017:27017
    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
      start_period: 10s
    command: "mongod --bind_ip_all --replSet rs0 --keyFile /tmp/key.file"
volumes:
  mongodb_data:

Same result.

Does anyone have a working docker definition with a replica set?

Hi @Hegyi_Gergely and welcome to the MongoDB community forum!!

Firstly, the image mentioned above does not seem valid MongoDB image. Can you confirm if this is a custom image created for the application.
However, please note that, there are two images:

  1. Docker
  2. Docker
    which are available at https://hub.docker.com/ and maintained by Docker and MongoDB respectively.

I tried to deploy a 3 node replica set using the docker-compose using the latest MongoDB image, and I was successfully able to run the deployment.

This is how my docker compose.yaml looks like:

services:
  mongo1:
    hostname: mongo1
    image: mongo
    expose:
      - 27017
    ports:
      - 30001:27017 
    restart: always
    command: mongod --replSet my-mongo-set
  mongo2:
    hostname: mongo2
    image: mongo
    expose:
      - 27017
    ports:
      - 30002:27017
    restart: always
    command: mongod --replSet my-mongo-set
  mongo3:
    hostname: mongo3
    image: mongo
    expose:
      - 27017
    ports:
      - 30003:27017
    restart: always
    command: mongod --replSet my-mongo-set

  mongoinit:
    image: mongo
    # this container will exit after executing the command
    restart: "no"
    depends_on:
      - mongo1
      - mongo2
      - mongo3
    command: >
      mongo --host mongo1:27017 --eval 
      '
      db = (new Mongo("localhost:27017")).getDB("test");
      config = {
      "_id" : "my-mongo-set",
      "members" : [
        {
          "_id" : 0,
          "host" : "mongo1:27017"
        },
        {
          "_id" : 1,
          "host" : "mongo2:27017"
        },
        {
          "_id" : 2,
          "host" : "mongo3:27017"
        }
      ]
      };
      rs.initiate(config);
      '
aasawari.sahasrabuddhe@M-C02DV42LML85 ~ % docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS                        PORTS                      NAMES
478b7e4f2dc1   mongo     "docker-entrypoint.s…"   15 seconds ago   Exited (127) 12 seconds ago                              aasawarisahasrabuddhe-mongoinit-1
761b79c62ef0   mongo     "docker-entrypoint.s…"   15 seconds ago   Up 13 seconds                 0.0.0.0:30003->27017/tcp   aasawarisahasrabuddhe-mongo3-1
24c7cdbfa412   mongo     "docker-entrypoint.s…"   15 seconds ago   Up 13 seconds                 0.0.0.0:30002->27017/tcp   aasawarisahasrabuddhe-mongo2-1
0de0fec70540   mongo     "docker-entrypoint.s…"   15 seconds ago   Up 13 seconds                 0.0.0.0:30001->27017/tcp   aasawarisahasrabuddhe-mongo1-1
aasawari.sahasrabuddhe@M-C02DV42LML85 ~ %

Let us know if you have any further questions.

Regards
Aasawari

Thanks for your answer!
I 'll try this way

The mongo6.0.5.t was a custom test Image:

FROM mongo:6.0.5
ENV TZ="Europe/Budapest"
ENV TIME_ZONE="Europe/Budapest"
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && dpkg-reconfigure -f noninteractive tzdata
RUN echo "rs.initiate({'_id':'rs0', members: [{'_id':1, 'host':'127.0.0.1:27017'}]});" > "/docker-entrypoint-initdb.d/init_replicaset.js"
RUN echo "xxxxxxxxxxxxxxxx" > "/tmp/key.file"
COPY ./init_replicaset.js /docker-entrypoint-initdb.d/init_replicaset.js
COPY ./mongo_init.js /docker-entrypoint-initdb.d/init_users.js
RUN chmod 600 /tmp/key.file
RUN chown 999:999 /tmp/key.file

CMD ["mongod", "--replSet", "rs0", "--bind_ip_all", "--keyFile", "/tmp/key.file"]

Dear Aasawari!

Sorry, but there are more problems with this example:
mongoinit: /usr/local/bin/docker-entrypoint.sh: line 420: exec: mongo: not found
The mongo replaced to mongosh in V6. Are you sure to use MongoDB V6?
you should update the lates mongo image:
docker pull mongo

mongo1…3:
“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”:3600}}

Hi Gergely!

In docker-compose:

  mongodb:
    container_name: mongodb
    image: mongo:latest
    expose:
      - 27017
    ports:
      - "27017:27017"
    volumes:
      - ./data/mongodb:/data/db
      - ./scripts/mongodb/rs-initiate.js:/docker-entrypoint-initdb.d/rs-initiate.js
    command: ["--replSet", "dbrs", "--bind_ip_all"]

in /scripts/mongodb/rs-initiate.js:

rs.initiate();
1 Like

YOU MADE MY DAY!!
Thank you for this Answer. This seems to work perfectly.

Is there also a way without providing a dedicated script?