docker-library / docker-library/mongo

[Bug]: mongo requires alternate shell in command to work as shard config server

Open
#717 9 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Shell
Stars
1.1k
Forks
658
Avg merge
4h 40m
Merged PRs (30d)
1

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
  1. Pull a MongoDB image (version 6, 7, or 8—it doesn't matter).
  2. Make the following mongod.conf:
sharding:
  clusterRole: configsvr
replication:
  replSetName: "cfg0"
  1. 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
  1. Start mongo with docker compose up.
  2. 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.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the shown mongod.conf and docker-compose.yml command and compare how the Docker image starts mongod with and without the /bin/sh -c workaround. Check the image entrypoint behavior and reproduce the config-server startup failure on MongoDB 6, 7, or 8. Done means the documented command starts a config server using the replica-set settings without requiring the shell workaround.

Written by the indexing model from the issue text.

Assessment

Tech stack
docker, docker-compose, mongodb, shell
Domain
databases, devops, infrastructure
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.