skip to Main Content

I am a Docker novice and have been trying for a long time, but I still cannot create the containers as expected.

This is my current configuration, but it does not meet the expectations: MongoServerError: no replset config has been received

version: "3.8"

services:
  RoteMongoFirst:
    image: mongo:5
    command: mongod --replSet roteReplicaSet --bind_ip localhost,RoteMongoFirst 
    container_name: RoteMongoFirst
    ports:
      - "27017:27017"
    environment:
      - MONGO_INITDB_DATABASE=Rote
      - MONGO_REPLICA_SET_MODE=primary
    volumes:
      - ./mongodb/RoteMongoFirst:/data/db
      - ./docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d

    networks:
      - rote-network

  RoteMongoSecond:
    image: mongo:5
    depends_on:
      - RoteMongoFirst
    command: >
      bash -c "mongod --replSet roteReplicaSet --bind_ip localhost,RoteMongoSecond &&
      mongosh --eval "rs.initiate({ _id: \"roteReplicaSet\", members: [ { _id: 0, host: \"RoteMongoFirst\" }, { _id: 1, host: \"RoteMongoSecond\" } ]})""
    container_name: RoteMongoSecond
    ports:
      - "27018:27017"
    environment:
      - MONGO_INITDB_DATABASE=Rote
      - MONGO_REPLICA_SET_MODE=secondary
    volumes:
      - ./mongodb/RoteMongoSecond:/data/db
      - ./docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d
    networks:
      - rote-network

networks:
  rote-network:

2

Answers


  1. Chosen as BEST ANSWER

    After spending a long time, I finally wrote a docker-compose file that meets expectations. It implements the creation of a MongoDB cluster and initializes a database (Rote) containing a collection (Rote). I hope it can help some people. 😎

    otherwise,Claude is really useful than gpt3.5

    version: "3"
    
    services:
      mongors1:
        image: mongo:7
        restart: always
        ports:
          - 27017:27017
        entrypoint: ["/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0"]
    
      mongors2:
        image: mongo:7
        restart: always
        entrypoint: ["/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0"]
    
      mongo-setup:
        image: mongo:4
        restart: on-failure
        entrypoint:
          - /bin/bash
          - -c
          - |
            mongo --host mongors1:27017 --eval 'rs.initiate({
              _id: "rs0",
              members: [
                {_id: 0, host: "mongors1:27017", priority: 2},
                {_id: 1, host: "mongors2:27017", priority: 1}
              ]
            })' && mongo --host mongors1:27017 --eval 'while (! db.hello().isWritablePrimary ) sleep(1000); db = db.getSiblingDB("Rote"); db.createCollection("Rote");'
        depends_on:
          - mongors1
          - mongors2
    
    

  2. Using multiple entries in --bind_ip often causes trouble. I would suggest to use either --bind_ip localhost or --bind_ip_all. Using --bind_ip with an IP makes only sense if your computer has multiple network interfaces. However, MongoDB does not support multiple network interfaces, so still it does not make much sense.

    Regarding you actual problem: You need to wait a bit till ReplicaSet is initiated. Try to add

    while (! db.hello().isWritablePrimary ) sleep(1000)
    

    after your rs.initiate(..) command.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search