I’ve used MongoDB for a while, but I’m relatively new to Docker. I am attempting to startup MongoDB in Docker for a playground area, but I’m having problems when trying to initiate the ReplicaSet via a docker-entrypoint-initdb.d JavaScript.
When using the following setup, the container starts correctly and I’m able to connect to the DB
docker-compose.yaml:
services:
mongodb0:
image: mongodb/mongodb-community-server:latest
container_name: MongoDB-TestRS-0
hostname: mongo-testrs-0
command:
- '--config'
- '/etc/mongod.conf'
volumes:
- ./mongod.conf:/etc/mongod.conf
- MongoDB-RS0-Data:/data/db
ports:
- '37017:27017'
volumes:
MongoDB-RS0-Data:
mongod.conf:
# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# Where and how to store data.
storage:
dbPath: /data/db
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /tmp/mongod.log
# network interfaces
net:
port: 27017
bindIp: 127.0.0.1
# how the process runs
processManagement:
timeZoneInfo: /usr/share/zoneinfo
# Security settings
# security:
# authorization: enabled
# keyFile: /opt/keyfile/mongo-keyfile
replication:
replSetName: TestRS
`
But with this setup, I need to manually connect to the db the first time and call rs.initiate()
. To avoid this, I created the following JavaScript file with the expectation that it would connect to the database and initialize the RS for me.
docker-entrypoint-initdb.d\mongo-init.js:
rs.initiate({ _id: 'TestRS', members: [ { _id: 0, host: 'localhost:27017' } // Add more members here ] });
And then added - ./docker-entrypoint-initdb.d/:/docker-entrypoint-initdb.d/:ro
as a volume entry in my docker-compose.yaml file.
However, it appears that before the docker-entrypoint.py
script will execute any of the scripts in docker-entrypoint-initdb.d
, it attempts to confirm the database is running by executing the command db.stats()
. Since this command can’t be executed until the replicaset has been initialized, I see the following output in the logs, then after many attempts, the container exits
MongoDB-TestRS-0 | MongoServerError: node is not in primary or recovering state MongoDB-TestRS-0 | {"t":{"$date":"2024-08-06T15:19:25.840+00:00"},"s":"I", "c":"NETWORK", "id":22944, "ctx":"conn115","msg":"Connection ended","attr":{"remote":"127.0.0.1:58598","uuid":{"uuid":{"$uuid":"8d4da918-1ce4-432f-92e3-ed1429cdd41e"}},"connectionId":115,"connectionCount":4}} MongoDB-TestRS-0 | {"t":{"$date":"2024-08-06T15:19:25.840+00:00"},"s":"I", "c":"NETWORK", "id":22944, "ctx":"conn111","msg":"Connection ended","attr":{"remote":"127.0.0.1:58568","uuid":{"uuid":{"$uuid":"ae572127-6bc5-473e-a0af-e4ccef511b2c"}},"connectionId":111,"connectionCount":3}} MongoDB-TestRS-0 | {"t":{"$date":"2024-08-06T15:19:25.840+00:00"},"s":"I", "c":"NETWORK", "id":22944, "ctx":"conn113","msg":"Connection ended","attr":{"remote":"127.0.0.1:58578","uuid":{"uuid":{"$uuid":"14c04b93-4b14-4066-9f1c-63bd119c43d1"}},"connectionId":113,"connectionCount":2}} MongoDB-TestRS-0 | {"t":{"$date":"2024-08-06T15:19:25.840+00:00"},"s":"I", "c":"NETWORK", "id":22944, "ctx":"conn112","msg":"Connection ended","attr":{"remote":"127.0.0.1:58576","uuid":{"uuid":{"$uuid":"7f4a0e9a-dad1-4ba7-b986-b23aa14647ad"}},"connectionId":112,"connectionCount":1}} MongoDB-TestRS-0 | {"t":{"$date":"2024-08-06T15:19:25.840+00:00"},"s":"I", "c":"NETWORK", "id":22944, "ctx":"conn114","msg":"Connection ended","attr":{"remote":"127.0.0.1:58590","uuid":{"uuid":{"$uuid":"e5184978-4d17-4a19-879c-007ef9d2f0d1"}},"connectionId":114,"connectionCount":0}} MongoDB-TestRS-0 | {"t":{"$date":"2024-08-06T15:19:26.001+00:00"},"s":"W", "c":"QUERY", "id":23799, "ctx":"ftdc","msg":"Aggregate command executor error","attr":{"error":{"code":26,"codeName":"NamespaceNotFound","errmsg":"Unable to retrieve storageStats in $collStats stage :: caused by :: Collection [local.oplog.rs] not found."},"stats":{},"cmd":{"aggregate":"oplog.rs","cursor":{},"pipeline":[{"$collStats":{"storageStats":{"waitForLock":false,"numericOnly":true}}}],"$db":"local"}}} MongoDB-TestRS-0 | {"t":{"$date":"2024-08-06T15:19:26.998+00:00"},"s":"I", "c":"NETWORK", "id":22943, "ctx":"listener","msg":"Connection accepted","attr":{"remote":"127.0.0.1:58602","uuid":{"uuid":{"$uuid":"76d2a139-e246-41ae-b95c-3c332f118caa"}},"connectionId":116,"connectionCount":1}} MongoDB-TestRS-0 | {"t":{"$date":"2024-08-06T15:19:27.001+00:00"},"s":"W", "c":"QUERY", "id":23799, "ctx":"ftdc","msg":"Aggregate command executor error","attr":{"error":{"code":26,"codeName":"NamespaceNotFound","errmsg":"Unable to retrieve storageStats in $collStats stage :: caused by :: Collection [local.oplog.rs] not found."},"stats":{},"cmd":{"aggregate":"oplog.rs","cursor":{},"pipeline":[{"$collStats":{"storageStats":{"waitForLock":false,"numericOnly":true}}}],"$db":"local"}}} MongoDB-TestRS-0 | {"t":{"$date":"2024-08-06T15:19:27.009+00:00"},"s":"I", "c":"NETWORK", "id":51800, "ctx":"conn116","msg":"client metadata","attr":{"remote":"127.0.0.1:58602","client":"conn116","negotiatedCompressors":[],"doc":{"application":{"name":"mongosh 2.2.15"},"driver":{"name":"nodejs|mongosh","version":"6.8.0|2.2.15"},"platform":"Node.js v20.16.0, LE","os":{"name":"linux","architecture":"x64","version":"3.10.0-327.22.2.el7.x86_64","type":"Linux"},"env":{"container":{"runtime":"docker"}}}}} MongoDB-TestRS-0 | {"t":{"$date":"2024-08-06T15:19:27.076+00:00"},"s":"I", "c":"NETWORK", "id":22943, "ctx":"listener","msg":"Connection accepted","attr":{"remote":"127.0.0.1:58610","uuid":{"uuid":{"$uuid":"54a6b940-05a9-43d2-84b5-4bb137dd0fe1"}},"connectionId":117,"connectionCount":2}} MongoDB-TestRS-0 | {"t":{"$date":"2024-08-06T15:19:27.076+00:00"},"s":"I", "c":"NETWORK", "id":22943, "ctx":"listener","msg":"Connection accepted","attr":{"remote":"127.0.0.1:58616","uuid":{"uuid":{"$uuid":"3464a9f8-46ef-4ba4-91f8-111af00e668b"}},"connectionId":118,"connectionCount":3}} MongoDB-TestRS-0 | {"t":{"$date":"2024-08-06T15:19:27.080+00:00"},"s":"I", "c":"NETWORK", "id":51800, "ctx":"conn117","msg":"client metadata","attr":{"remote":"127.0.0.1:58610","client":"conn117","negotiatedCompressors":[],"doc":{"application":{"name":"mongosh 2.2.15"},"driver":{"name":"nodejs|mongosh","version":"6.8.0|2.2.15"},"platform":"Node.js v20.16.0, LE","os":{"name":"linux","architecture":"x64","version":"3.10.0-327.22.2.el7.x86_64","type":"Linux"},"env":{"container":{"runtime":"docker"}}}}} MongoDB-TestRS-0 | {"t":{"$date":"2024-08-06T15:19:27.081+00:00"},"s":"I", "c":"NETWORK", "id":51800, "ctx":"conn118","msg":"client metadata","attr":{"remote":"127.0.0.1:58616","client":"conn118","negotiatedCompressors":[],"doc":{"application":{"name":"mongosh 2.2.15"},"driver":{"name":"nodejs|mongosh","version":"6.8.0|2.2.15"},"platform":"Node.js v20.16.0, LE","os":{"name":"linux","architecture":"x64","version":"3.10.0-327.22.2.el7.x86_64","type":"Linux"},"env":{"container":{"runtime":"docker"}}}}} MongoDB-TestRS-0 | {"t":{"$date":"2024-08-06T15:19:27.083+00:00"},"s":"I", "c":"NETWORK", "id":6788700, "ctx":"conn117","msg":"Received first command on ingress connection since session start or auth handshake","attr":{"elapsedMillis":3}} MongoDB-TestRS-0 | {"t":{"$date":"2024-08-06T15:19:27.087+00:00"},"s":"I", "c":"NETWORK", "id":22943, "ctx":"listener","msg":"Connection accepted","attr":{"remote":"127.0.0.1:58644","uuid":{"uuid":{"$uuid":"11835e63-9ab6-48b3-aaea-abf6a2f2e926"}},"connectionId":120,"connectionCount":5}} MongoDB-TestRS-0 | {"t":{"$date":"2024-08-06T15:19:27.088+00:00"},"s":"I", "c":"NETWORK", "id":51800, "ctx":"conn119","msg":"client metadata","attr":{"remote":"127.0.0.1:58628","client":"conn119","negotiatedCompressors":[],"doc":{"application":{"name":"mongosh 2.2.15"},"driver":{"name":"nodejs|mongosh","version":"6.8.0|2.2.15"},"platform":"Node.js v20.16.0, LE","os":{"name":"linux","architecture":"x64","version":"3.10.0-327.22.2.el7.x86_64","type":"Linux"},"env":{"container":{"runtime":"docker"}}}}} MongoDB-TestRS-0 | {"t":{"$date":"2024-08-06T15:19:27.091+00:00"},"s":"I", "c":"NETWORK", "id":51800, "ctx":"conn120","msg":"client metadata","attr":{"remote":"127.0.0.1:58644","client":"conn120","negotiatedCompressors":[],"doc":{"application":{"name":"mongosh 2.2.15"},"driver":{"name":"nodejs|mongosh","version":"6.8.0|2.2.15"},"platform":"Node.js v20.16.0, LE","os":{"name":"linux","architecture":"x64","version":"3.10.0-327.22.2.el7.x86_64","type":"Linux"},"env":{"container":{"runtime":"docker"}}}}} MongoDB-TestRS-0 | {"t":{"$date":"2024-08-06T15:19:27.092+00:00"},"s":"I", "c":"NETWORK", "id":6788700, "ctx":"conn119","msg":"Received first command on ingress connection since session start or auth handshake","attr":{"elapsedMillis":4}} MongoDB-TestRS-0 | MongoServerError: node is not in primary or recovering state MongoDB-TestRS-0 | {"t":{"$date":"2024-08-06T15:19:27.108+00:00"},"s":"I", "c":"NETWORK", "id":22944, "ctx":"conn118","msg":"Connection ended","attr":{"remote":"127.0.0.1:58616","uuid":{"uuid":{"$uuid":"3464a9f8-46ef-4ba4-91f8-111af00e668b"}},"connectionId":118,"connectionCount":4}} MongoDB-TestRS-0 | {"t":{"$date":"2024-08-06T15:19:27.109+00:00"},"s":"I", "c":"NETWORK", "id":22944, "ctx":"conn120","msg":"Connection ended","attr":{"remote":"127.0.0.1:58644","uuid":{"uuid":{"$uuid":"11835e63-9ab6-48b3-aaea-abf6a2f2e926"}},"connectionId":120,"connectionCount":3}} MongoDB-TestRS-0 | {"t":{"$date":"2024-08-06T15:19:27.109+00:00"},"s":"I", "c":"NETWORK", "id":22944, "ctx":"conn116","msg":"Connection ended","attr":{"remote":"127.0.0.1:58602","uuid":{"uuid":{"$uuid":"76d2a139-e246-41ae-b95c-3c332f118caa"}},"connectionId":116,"connectionCount":2}} MongoDB-TestRS-0 | {"t":{"$date":"2024-08-06T15:19:27.109+00:00"},"s":"I", "c":"NETWORK", "id":22944, "ctx":"conn119","msg":"Connection ended","attr":{"remote":"127.0.0.1:58628","uuid":{"uuid":{"$uuid":"49a33ba5-662b-4eea-8b31-661f475edb30"}},"connectionId":119,"connectionCount":1}} MongoDB-TestRS-0 | {"t":{"$date":"2024-08-06T15:19:27.109+00:00"},"s":"I", "c":"NETWORK", "id":22944, "ctx":"conn117","msg":"Connection ended","attr":{"remote":"127.0.0.1:58610","uuid":{"uuid":{"$uuid":"54a6b940-05a9-43d2-84b5-4bb137dd0fe1"}},"connectionId":117,"connectionCount":0}} MongoDB-TestRS-0 | {"t":{"$date":"2024-08-06T15:19:28.001+00:00"},"s":"W", "c":"QUERY", "id":23799, "ctx":"ftdc","msg":"Aggregate command executor error","attr":{"error":{"code":26,"codeName":"NamespaceNotFound","errmsg":"Unable to retrieve storageStats in $collStats stage :: caused by :: Collection [local.oplog.rs] not found."},"stats":{},"cmd":{"aggregate":"oplog.rs","cursor":{},"pipeline":[{"$collStats":{"storageStats":{"waitForLock":false,"numericOnly":true}}}],"$db":"local"}}} MongoDB-TestRS-0 | Take a look at your mongod configuration to see if something is wrong. MongoDB-TestRS-0 | Warning: mongod not running yet. MongoDB-TestRS-0 | Warning: mongod not running yet. ... MongoDB-TestRS-0 | Warning: mongod not running yet. MongoDB-TestRS-0 | error: mongod still not running after 30 second(s). MongoDB-TestRS-0 exited with code 1
Is there another way I can/should call the rs.initiate()
command?
Would it be acceptable to have the docker-entrypoint.ph script updated to call the db.hello()
command instead which doesn’t fail if replicaset is enabled, but not yet initialized?