docker-library / mongo

Docker Official Image packaging for MongoDB
https://www.mongodb.org/
Apache License 2.0
1.03k stars 620 forks source link

Mongos should be allowed to run initdb.d scripts too #365

Closed ravanscafi closed 6 months ago

ravanscafi commented 5 years ago

Basically, I'm trying to configure shards when running mongos:

sh.addShard("rs1/db")

But the scripts in docker-entrypoint-initdb.d only run for mongod:

https://github.com/docker-library/mongo/blob/59fba668ae47e3ec7f7917a56db4f94d6d774765/3.6/docker-entrypoint.sh#L192-L318

Does it make sense to run for mongos too?

wglambert commented 5 years ago

Is this a duplicate of https://github.com/docker-library/mongo/issues/339?

https://github.com/docker-library/mongo/issues/339#issuecomment-485578503 The docker-entrypoint-initdb.d scripts run during an initialization period (and only if the database is empty), during which the container is only listening on localhost so trying to initiate a cluster during that period isn't possible as it won't resolve it's own container hostname.

ravanscafi commented 5 years ago

Actually I did a pretty big jerry rig and was able to configure it, but I did from other container running mongod, so it would work with mongos

The hack is nohup the script with an sleep, so the command runs and the servers are started, then the config is applied.

something like this:

nohup bash -c "sleep 10 && echo 'rs.initiate({_id: \"mongors1\", members: [{_id: 0, host: \"db\"}]})' | mongo --host db" &

It's really really ugly, but it works. So I'm not sure how exactly related it is with the other issue, but having the ability to run scripts with mongos too would simplify my workflow.

Shogoki commented 5 years ago

Not sure, if this is directly related, but i had a similiar issue. I wanted to initialize a replicaset in my local dev container. I ended up with the following workaround.

#!/bin/bash

if [ "$1" != "CHILD" ]
    then
    # we are the main process so we need to start our child
    echo "STARTING CHILD"
    /bin/bash $BASH_SOURCE CHILD &
else
    # we are the CHILD so we run the actual init code:
    echo "Starting BG init script. Wating for Mongo to listen on $MONGO_HOST"
    ## waiting for mongo to be available
    until nc -z $MONGO_HOST 27017
    do
        sleep 1
    done
    echo "going on.. Initialize replset"
    # initializing replicaset
    mongo --eval  "rs.initiate({ _id: \"$MONGO_REPLSET\", version: 1,  members: [  { _id: 0, host : \"$MONGO_HOST\" },  ] });"

    mongo --eval 'rs.status()'
fi  

This required me to set MONGO_HOST to the name of the service (in my compose file) and MOGO_REPLSET to the replSet Name.

alitoufighi commented 4 years ago

I actually didn't figure out how the workarounds mentioned here can be used to run a mongos command in initialization step, but I have the same issue. If anybody can explain what I can do to add a shard automatically on startup, I would be so grateful.

I need to setup a sharded cluster that has the config server and shards up and running. The only requirement here is to run a mongos container and perform sh.addShard() on it.