-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat: simplify MongoDB initialization in docker-compose #3633
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: simplify MongoDB initialization in docker-compose #3633
Conversation
|
💕 Thank you for your contribution and please kindly read and sign our CLA. CLA Docs I have read the CLA Document and I hereby sign the CLA You can retrigger this bot by commenting recheck in this Pull Request. Posted by the CLA Assistant Lite bot. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR refactors MongoDB initialization in docker-compose by replacing a complex inline bash script with a dedicated initialization script. The changes leverage MongoDB's built-in /docker-entrypoint-initdb.d/ mechanism for better maintainability and readability.
Key Changes:
- Created
scripts/mongo-init.shfor MongoDB user initialization - Simplified MongoDB service configuration by removing inline bash command
- Cleaned up Redis command formatting for consistency
- Removed extraneous whitespace in docker-compose.yml
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| scripts/mongo-init.sh | New dedicated script for creating MongoDB OpenIM user using mongosh |
| docker-compose.yml | Simplified MongoDB service by mounting init script, refactored command structure, and cleaned up formatting |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
scripts/mongo-init.sh
Outdated
| db.createUser({ | ||
| user: "${MONGO_OPENIM_USERNAME}", | ||
| pwd: "${MONGO_OPENIM_PASSWORD}", | ||
| roles: [{role: "readWrite", db: "${MONGO_INITDB_DATABASE}"}] | ||
| }) | ||
| print("OpenIM user created successfully") |
Copilot
AI
Dec 3, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The MongoDB init script doesn't check if the user already exists before attempting to create it. Unlike the original inline script which included a !db.getUser() check, this new script will fail on subsequent container restarts when the data volume persists and the user already exists.
Consider adding idempotency by checking if the user exists first:
use ${MONGO_INITDB_DATABASE}
if (!db.getUser("${MONGO_OPENIM_USERNAME}")) {
db.createUser({
user: "${MONGO_OPENIM_USERNAME}",
pwd: "${MONGO_OPENIM_PASSWORD}",
roles: [{role: "readWrite", db: "${MONGO_INITDB_DATABASE}"}]
})
print("OpenIM user created successfully")
} else {
print("User already exists: ${MONGO_OPENIM_USERNAME}")
}| db.createUser({ | |
| user: "${MONGO_OPENIM_USERNAME}", | |
| pwd: "${MONGO_OPENIM_PASSWORD}", | |
| roles: [{role: "readWrite", db: "${MONGO_INITDB_DATABASE}"}] | |
| }) | |
| print("OpenIM user created successfully") | |
| if (!db.getUser("${MONGO_OPENIM_USERNAME}")) { | |
| db.createUser({ | |
| user: "${MONGO_OPENIM_USERNAME}", | |
| pwd: "${MONGO_OPENIM_PASSWORD}", | |
| roles: [{role: "readWrite", db: "${MONGO_INITDB_DATABASE}"}] | |
| }) | |
| print("OpenIM user created successfully") | |
| } else { | |
| print("User already exists: ${MONGO_OPENIM_USERNAME}") | |
| } |
- Replace complex inline bash script with dedicated init script - Use MongoDB built-in /docker-entrypoint-initdb.d/ mechanism - Create scripts/mongo-init.sh for user creation - Improve readability and maintainability Fixes openimsdk#3632
49a61f8 to
686366b
Compare
Fixes #3632