mongodbdockerinitializationreplicaset

Automatically initialize replica set for mongoDB in docker fails


I have a NodeJS Express App that depends on MongoDB change streams. For them to be available, MongoDB has to be configured to run as a replica set (even if there is only one node in that set).

I'm working on Windows 10 pro.

I'm trying to dockerize this App, basing the MongoDB container off the official mongo:5 image.

For this to work, I want an automated way of initializing the DB as a replica set. Tutorials I've found rely on either execing into the container and running rs.initiate() from mongosh (or similar approaches), which is manual work I want to avoid. Or they use hacks like wait-for-it.sh as here.

I feel there must be a better solution, based somehow on the paragraph "Initializing a fresh instance", from the docs.

It describes that

When a container is started for the first time it will execute files with extensions .sh and .js that are found in /docker-entrypoint-initdb.d.

When exactly in the container lifecycle does that happen? After the container is initialized? Or after the DB is ready? Because this seems to be the perfect place for this initialization logic, which runs flawlessly when executed manually, from within the container.

However, placing

// initReplSet.js
print('Script running');
config={"_id":"rs0", "members":[{"_id":0,"host":"app-db:27017"}]};
print(JSON.stringify(rs.initiate(config)));
print('Script end');

fails with the error {"ok":0,"errmsg":"No host described in new configuration with {version: 1, term: 0} for replica set rs0 maps to this node","code":93,"codeName":"InvalidReplicaSetConfig"}, yet the database is available under the hostname app-db from other containers. This makes me feel that this code runs too early, before all other initialization logic (networking) is done.

Another approach is to place a bash script that executes code via mongosh. Here's what I've tried:

#!/bin/bash
mongosh "mongodb://app-db:27017/app_db" "initiateReplSet"

where initiateReplSet is

config={"_id":"rs0", "members":[{"_id":0,"host":"app-db:27017"}]}
rs.initiate(config)
exit

but this crashes the container with the error

/usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/initiateReplSetWrapper.sh

{"t":{"$date":"2022-02-15T11:31:23.353+00:00"},"s":"I",  "c":"-",        "id":4939300, "ctx":"monitoring-keys-for-HMAC","msg":"Failed to refresh key cache","attr":{"error":"NotYetInitialized: Cannot use non-local read concern until replica set is finished initializing.","nextWakeupMillis":600}}

Warning: Could not access file: EACCES: permission denied, mkdir '/home/mongodb'

Current Mongosh Log ID: 620b8f0b04b7ad69b446768d

Connecting to: mongodb://app-db:27017/app_db?directConnection=true&appName=mongosh+1.1.9

Only the first and the last three lines seem to really belong to the bash script, the second line is repeated constantly.

I'm not sure whether the error originates at the permission denied issue, or whether the DB really can't be accessed. However, specifying

RUN mkdir -p /home/mongodb/.mongodb
RUN chown -R 777 /home/mongodb

in the Dockerfile did not improve the situation (same error nevertheless).

Could you please explain either why this approach can not work, or how to make it work? Is there another, better, automated way to initialize the replica set? Could the docker image be improved to allow such initialization logic?


Solution

  • I just made it work with a wild experiment. Means I simply left out the config in my call to rs.initiate(), from the JS script. For some reason, the script then runs successfully and change streams become available to my NodeJS backend.

    I will post everything that's needed to run a MongoDB docker with change streams enabled:

    # Dockerfile
    From mongo
    WORKDIR .
    COPY initiateReplSet.js ./docker-entrypoint-initdb.d/
    CMD ["-replSet", "rs0"]
    
    // initiateReplSet.js
    rs.initiate()