MongoDB 6.0.4 cluster on docker installation issue

Hello,

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

Thanks in advance

I have the same issue when i tried to deploy config server cluster with kubernetes on azure. I setup environment variables: MONGO_INITDB_ROOT_USERNAME and MONGO_INITDB_ROOT_PASSWORD. And the args list is: args: [“–keyFile”, “/mongodb/replicaset.key”, “–configsvr”, “–replSet”, “rs0”, “–dbpath”, “/data/configdb”]. But deployment failed with such error:
BadValue: Cannot start a configsvr as a standalone server. Please use the option --replSet to start the node as a replica set.
try ‘mongod --help’ for more information

I finally may have found something.

Seems it is normal that it fails on shard server.
For config server, there is a PR: Fix configsvr with user/pass env vars on 5.0 & 6.0 by yosifkit · Pull Request #600 · docker-library/mongo · GitHub
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

So currently is it impossible for me to deploy sharded cluster on azure container with mongodb5/6 community? Starting each member of the config server replica set always fails with such error: “BadValue: Cannot start a configsvr as a standalone server” even though I have provided “–replSet” option when start it.

I found this repository: GitHub - minhhungit/mongodb-cluster-docker-compose: Demo a simple sharded Mongo Cluster with a replication using docker compose
I am not done testing it but it seems to work for cluster and user initialization.

Hi Ankou,
Thank you for your reply!
I am deploying sharded cluster with aks not docker. I tried to pull the latest mongodb image. But it doesn’t work yet.

Did you ever figure this out?

We’re on MongoDB 8 now, and I still get this same error on a new mongo image with a config file as simple as:

replication:
 replSetName: "cfg0"
sharding:
  clusterRole: configsvr