skip to Main Content

I am currently trying to install a mongo cluster on docker.
We already have such cluster with mongo 4.2 but for the new installation we wanted to use latest version of docker image.
I used the same docker-compose file but the data and config servers don’t want to start.
When looking at the docker logs, the error is:

BadValue: Cannot start a shardsvr as a standalone server. Please use the option --replSet to start the node as a replica set.

BadValue: Cannot start a configsvr as a standalone server. Please use the option --replSet to start the node as a replica set.

But I have the replSet in my commands.

After some try and errors, the error occurs when I add the init db environment variables to initialize the admin user.

environment:
      MONGO_INITDB_ROOT_USERNAME: ${MONGO_ADMIN_USER}
      MONGO_INITDB_ROOT_PASSWORD: ${MONGO_ADMIN_PASSWORD}

I did the test also with mongo image version 5 and I have same behavior.
I works fine with mongo image 4.4.18

Here is my docker compose file

version: '3.5'
services:
  # Router
  mongo-router-01:
    command: mongos --port 27017 --configdb ${MONGO_RS_CONFIG_NAME}/mongo-config-01:27017,mongo-config-02:27017,mongo-config-03:27017 --bind_ip_all --keyFile /etc/mongo-cluster.key
    container_name: ${MONGO_ROUTER_SERVER}-01-${ENVIRONMENT_NAME}
    environment:
      MONGO_INITDB_ROOT_USERNAME: ${MONGO_ADMIN_USER}
      MONGO_INITDB_ROOT_PASSWORD: ${MONGO_ADMIN_PASSWORD}
    image: mongo:${MONGO_VERSION}
    networks:
      - mongo-network
    restart: always
    volumes:
      - ./keys/${ENVIRONMENT_NAME}/mongo-cluster.key:/etc/mongo-cluster.key
      - ./volumes/${ENVIRONMENT_NAME}/${MONGO_ROUTER_SERVER}-01/db:/data/db
      - ./volumes/${ENVIRONMENT_NAME}/${MONGO_ROUTER_SERVER}-01/configdb:/data/configdb
  mongo-router-02:
    command: mongos --port 27017 --configdb ${MONGO_RS_CONFIG_NAME}/mongo-config-01:27017,mongo-config-02:27017,mongo-config-03:27017 --bind_ip_all --keyFile /etc/mongo-cluster.key
    container_name: ${MONGO_ROUTER_SERVER}-02-${ENVIRONMENT_NAME}
    environment:
      MONGO_INITDB_ROOT_USERNAME: ${MONGO_ADMIN_USER}
      MONGO_INITDB_ROOT_PASSWORD: ${MONGO_ADMIN_PASSWORD}
    image: mongo:${MONGO_VERSION}
    networks:
      - mongo-network
    restart: always
    volumes:
      - ./keys/${ENVIRONMENT_NAME}/mongo-cluster.key:/etc/mongo-cluster.key
      - ./volumes/${ENVIRONMENT_NAME}/${MONGO_ROUTER_SERVER}-02/db:/data/db
      - ./volumes/${ENVIRONMENT_NAME}/${MONGO_ROUTER_SERVER}-02/configdb:/data/configdb
  
  # Config Servers
  mongo-config-01:
    command: mongod --port 27017 --configsvr --replSet ${MONGO_RS_CONFIG_NAME} --keyFile /etc/mongo-cluster.key
    container_name: ${MONGO_CONFIG_SERVER}-01-${ENVIRONMENT_NAME}
    environment:
      MONGO_INITDB_ROOT_USERNAME: ${MONGO_ADMIN_USER}
      MONGO_INITDB_ROOT_PASSWORD: ${MONGO_ADMIN_PASSWORD}
    image: mongo:${MONGO_VERSION}
    networks:
      - mongo-network
    restart: always
    volumes:
      - ./keys/preprod/mongo-cluster.key:/etc/mongo-cluster.key
      - ./volumes/${ENVIRONMENT_NAME}/${MONGO_CONFIG_SERVER}-01/db:/data/db
      - ./volumes/${ENVIRONMENT_NAME}/${MONGO_CONFIG_SERVER}-01/configdb:/data/configdb
  mongo-config-02:
    command: mongod --port 27017 --configsvr --replSet ${MONGO_RS_CONFIG_NAME} --keyFile /etc/mongo-cluster.key
    container_name: ${MONGO_CONFIG_SERVER}-02-${ENVIRONMENT_NAME}
    environment:
      MONGO_INITDB_ROOT_USERNAME: ${MONGO_ADMIN_USER}
      MONGO_INITDB_ROOT_PASSWORD: ${MONGO_ADMIN_PASSWORD}
    image: mongo:${MONGO_VERSION}
    networks:
      - mongo-network
    restart: always
    volumes:
      - ./keys/preprod/mongo-cluster.key:/etc/mongo-cluster.key
      - ./volumes/${ENVIRONMENT_NAME}/${MONGO_CONFIG_SERVER}-02/db:/data/db
      - ./volumes/${ENVIRONMENT_NAME}/${MONGO_CONFIG_SERVER}-02/configdb:/data/configdb
  mongo-config-03:
    command: mongod --port 27017 --configsvr --replSet ${MONGO_RS_CONFIG_NAME} --keyFile /etc/mongo-cluster.key
    container_name: ${MONGO_CONFIG_SERVER}-03-${ENVIRONMENT_NAME}
    environment:
      MONGO_INITDB_ROOT_USERNAME: ${MONGO_ADMIN_USER}
      MONGO_INITDB_ROOT_PASSWORD: ${MONGO_ADMIN_PASSWORD}
    image: mongo:${MONGO_VERSION}
    networks:
      - mongo-network
    restart: always
    volumes:
      - ./keys/${ENVIRONMENT_NAME}/mongo-cluster.key:/etc/mongo-cluster.key
      - ./volumes/${ENVIRONMENT_NAME}/${MONGO_CONFIG_SERVER}-03/db:/data/db
      - ./volumes/${ENVIRONMENT_NAME}/${MONGO_CONFIG_SERVER}-03/configdb:/data/configdb
  
  # Data Servers   
  mongo-arbiter-01:
    command: mongod --port 27017 --shardsvr --replSet ${MONGO_RS_DATA_NAME} --keyFile /etc/mongo-cluster.key
    container_name: ${MONGO_ARBITER_SERVER}-01-${ENVIRONMENT_NAME}
    environment:
      MONGO_INITDB_ROOT_USERNAME: ${MONGO_ADMIN_USER}
      MONGO_INITDB_ROOT_PASSWORD: ${MONGO_ADMIN_PASSWORD}
    image: mongo:${MONGO_VERSION}
    networks:
      - mongo-network
    restart: always
    volumes:
      - ./keys/${ENVIRONMENT_NAME}/mongo-cluster.key:/etc/mongo-cluster.key
      - ./volumes/${ENVIRONMENT_NAME}/${MONGO_ARBITER_SERVER}-01/db:/data/db
      - ./volumes/${ENVIRONMENT_NAME}/${MONGO_ARBITER_SERVER}-01/configdb:/data/configdb
  mongo-data-01:
    command: mongod --port 27017 --shardsvr --replSet ${MONGO_RS_DATA_NAME} --keyFile /etc/mongo-cluster.key
    container_name: ${MONGO_DATA_SERVER}-01-${ENVIRONMENT_NAME}
    environment:
      MONGO_INITDB_ROOT_USERNAME: ${MONGO_ADMIN_USER}
      MONGO_INITDB_ROOT_PASSWORD: ${MONGO_ADMIN_PASSWORD}
    image: mongo:${MONGO_VERSION}
    networks:
      - mongo-network
    restart: always
    volumes:
      - ./keys/${ENVIRONMENT_NAME}/mongo-cluster.key:/etc/mongo-cluster.key
      - ./volumes/${ENVIRONMENT_NAME}/${MONGO_DATA_SERVER}-01/db:/data/db
      - ./volumes/${ENVIRONMENT_NAME}/${MONGO_DATA_SERVER}-01/configdb:/data/configdb
  mongo-data-02:
    command: mongod --port 27017 --shardsvr --replSet ${MONGO_RS_DATA_NAME} --keyFile /etc/mongo-cluster.key
    container_name: ${MONGO_DATA_SERVER}-02-${ENVIRONMENT_NAME}
    environment:
      MONGO_INITDB_ROOT_USERNAME: ${MONGO_ADMIN_USER}
      MONGO_INITDB_ROOT_PASSWORD: ${MONGO_ADMIN_PASSWORD}
    image: mongo:${MONGO_VERSION}    
    networks:
      - mongo-network
    restart: always
    volumes:
      - ./keys/${ENVIRONMENT_NAME}/mongo-cluster.key:/etc/mongo-cluster.key
      - ./volumes/${ENVIRONMENT_NAME}/${MONGO_DATA_SERVER}-02/db:/data/db
      - ./volumes/${ENVIRONMENT_NAME}/${MONGO_DATA_SERVER}-02/configdb:/data/configdb
networks:
  mongo-network:
    external:
      name: _preprod

EDIT 2023-02-08

I finally may have found something: https://github.com/docker-library/mongo/issues/509

Seems it is normal that it fails on shard server.
For config server, there is a PR: https://github.com/docker-library/mongo/pull/600

But it has not been merged yet.

So I guess until the PR is merged and new version of the image is published, there is no way to use the environment variables at all.

So the root user insertion should be done via script after the replica sets and routers are initialized

2

Answers


  1. Chosen as BEST ANSWER

    I found this repository: https://github.com/minhhungit/mongodb-cluster-docker-compose

    There are some docker-compose files (with or without cluster key) I tested it and it seems to work.

    EDIT 13/02/2023

    Took a bit more time than expected as I do not have the same setup as the example found in the repository mentioned above, but now all is working fine with replica set and users initialized correctly


  2. I am not familiar with Docker, so I can only guess. When I deployed my sharded cluster I had to add readPreference=primaryPreferred to connectionString.

    Otherwise when the ReplicaSet is initated, then the current host may become a SECONDARY and the shell does not switch-over automatically to new PRIMARY.

    Another common issue, is when the ReplicaSet is initated then you must wait till it is finished before you run other actions. When I initate a ReplicaSet, then usually I do it like this:

    rs.initiate(...)
    while (! db.hello().isWritablePrimary ) { sleep(1000) }
    

    And last but not least in version 6.0 you must set setDefaultRWConcern before you can run many other operations, see Compatibility Changes in MongoDB 6.0#Replica Sets

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