Description
Error Message and Logs
BadValue: Cannot start a configsvr as a standalone server. Please use the option --replSet to start the node as a replica set.
Steps to Reproduce
- Pull a MongoDB image (version 6, 7, or 8—it doesn't matter).
- Make the following
mongod.conf
:
sharding:
clusterRole: configsvr
replication:
replSetName: "cfg0"
- Create a
docker-compose.yml
file for starting mongo similar to this (simplified for illustration):
services:
mongocfg1:
image: 'mongo:8'
command: 'mongod --config /etc/mongo/mongod.conf'
container_name: mongocfg1
# ...
volumes:
- 'mongodb-configdb:/data/configdb'
- 'mongodb-db:/data/db'
-
type: bind
source: /data/mongod.conf
target: /etc/mongo/mongod.conf
read_only: true
volumes:
mongodb-configdb:
name: mongodb-configdb
external: false
mongodb-db:
name: mongodb-db
external: false
- Start mongo with
docker compose up
. - MongoDB will not start and will complain that the replica set option is missing, even though it's clearly there in the
mongod.conf
.
Additional Information
After a lot of digging and experimenting, I found the strangest cause for this. It's the command
line of docker-compose.yml
. Whether I make it command: 'mongod --config /etc/mongo/mongod.conf'
or command: --config /etc/mongo/mongod.conf
, it will not read the replica set options when including the configsvr
option. And it doesn't seem to matter what I change in mongod.conf
.
However, if we replace the command
line with:
command: "/bin/sh -c 'exec mongod --config /etc/mongo/mongod.conf'"
Or:
entrypoint: "/bin/sh"
command: "-c 'exec mongod --config /etc/mongo/mongod.conf'"
And then run docker compose up
, mongod
appears to start fine.
Aside: even though the error message is complaining about missing replica set configuration, adding --replSet NAME
to the command
line still didn't fix it. It seems the only way to use Docker to start a shard config server is by doing the shell trick as shown above.
Although I've found this workaround, this is not the way the Docker image documentation says to do it, and it's not possible with systems that take control of docker-compose.yml
(such as Coolify), forcing it back to command: 'mongod --config /etc/mongo/mongod.conf'
, which should work anyway!
For reference, here's the MongoDB documentation on creating a shard config server. Yes, it points out binding an IP, but I've noticed that makes no difference with this problem.