I use MongoDB change stream in my code and need to create a MongoDB docker image with change stream enabled. The problem is that mongod should be started first with default settings to allow create users, documents, etc. Then mongod should be stopped. Then the replica set should be added to the mongod.conf to enable change stream:
# mongod.conf
replication:
replSetName: rs0
oplogSizeMB: 100
After that mongod should be started again and replica set initialized by MongoDB shell:
rs.slaveOk()
rs.initiate()
rs.initiate({_id:"rs0", members: [{"_id":1, "host":"127.0.0.1:27017"}]})
MogodDB 3.6 base image provides an initialization capabilities. Do you know how to start mongod, initialize DB then stop it and reconfigure?
UPD: I need to initialize database then add replica set. Therefore, I need to run the mongod with the default mongod.conf, create users and collections, then restart the mongod with another mongod,conf in which the replica set is enabled. I can't do that with official MongoDB image. I've installed MongoDB 3.6.12 on Ubuntu image. My MongoDB container is working well after running the setup commands manually in its bash shell, but the same instructions not working from Dockerfile Here is the commands
RUN mongod --fork --config /etc/mongod.conf \
&& mongo < /opt/init_mongodb.js \
&& mongod --shutdown --dbpath /var/lib/mongodb \
&& cp /etc/mongod.conf /etc/mongod.conf.orig \
&& mongod --fork --config /opt/mongod.conf \
&& mongo -u "root" -p "root" --authenticationDatabase "admin" < /opt/reconfig_mongodb.js \
When run this commands from Dockerfile, the following error appears
> backend@1.0.0 start /usr/src/app
> npm run babelserver
> backend@1.0.0 babelserver /usr/src/app
> babel-node --presets es2015 index.js
(node:41) UnhandledPromiseRejectionWarning: MongoError: not master and slaveOk=false
at queryCallback (/usr/src/app/node_modules/mongodb-core/lib/cursor.js:248:25)
at /usr/src/app/node_modules/mongodb-core/lib/connection/pool.js:532:18
at processTicksAndRejections (internal/process/task_queues.js:82:9)
(node:41) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)