Skip to content

Commit 686366b

Browse files
committed
feat: simplify MongoDB initialization in docker-compose
- 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 #3632
1 parent b8c4b45 commit 686366b

File tree

2 files changed

+23
-33
lines changed

2 files changed

+23
-33
lines changed

docker-compose.yml

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,44 +8,20 @@ services:
88
ports:
99
- "37017:27017"
1010
container_name: mongo
11-
command: >
12-
bash -c '
13-
docker-entrypoint.sh mongod --wiredTigerCacheSizeGB $$wiredTigerCacheSizeGB --auth &
14-
until mongosh -u $$MONGO_INITDB_ROOT_USERNAME -p $$MONGO_INITDB_ROOT_PASSWORD --authenticationDatabase admin --eval "db.runCommand({ ping: 1 })" &>/dev/null; do
15-
echo "Waiting for MongoDB to start..."
16-
sleep 1
17-
done &&
18-
mongosh -u $$MONGO_INITDB_ROOT_USERNAME -p $$MONGO_INITDB_ROOT_PASSWORD --authenticationDatabase admin --eval "
19-
db = db.getSiblingDB(\"$$MONGO_INITDB_DATABASE\");
20-
if (!db.getUser(\"$$MONGO_OPENIM_USERNAME\")) {
21-
db.createUser({
22-
user: \"$$MONGO_OPENIM_USERNAME\",
23-
pwd: \"$$MONGO_OPENIM_PASSWORD\",
24-
roles: [{role: \"readWrite\", db: \"$$MONGO_INITDB_DATABASE\"}]
25-
});
26-
print(\"User created successfully: \");
27-
print(\"Username: $$MONGO_OPENIM_USERNAME\");
28-
print(\"Password: $$MONGO_OPENIM_PASSWORD\");
29-
print(\"Database: $$MONGO_INITDB_DATABASE\");
30-
} else {
31-
print(\"User already exists in database: $$MONGO_INITDB_DATABASE, Username: $$MONGO_OPENIM_USERNAME\");
32-
}
33-
" &&
34-
tail -f /dev/null
35-
'
3611
volumes:
3712
- "${DATA_DIR}/components/mongodb/data/db:/data/db"
3813
- "${DATA_DIR}/components/mongodb/data/logs:/data/logs"
3914
- "${DATA_DIR}/components/mongodb/data/conf:/etc/mongo"
4015
- "${MONGO_BACKUP_DIR}:/data/backup"
16+
- ./scripts/mongo-init.sh:/docker-entrypoint-initdb.d/mongo-init.sh:ro
4117
environment:
4218
- TZ=Asia/Shanghai
43-
- wiredTigerCacheSizeGB=1
4419
- MONGO_INITDB_ROOT_USERNAME=root
4520
- MONGO_INITDB_ROOT_PASSWORD=openIM123
4621
- MONGO_INITDB_DATABASE=openim_v3
4722
- MONGO_OPENIM_USERNAME=openIM
4823
- MONGO_OPENIM_PASSWORD=openIM123
24+
command: [ "mongod", "--wiredTigerCacheSizeGB", "1", "--auth" ]
4925
restart: always
5026
networks:
5127
- openim
@@ -64,11 +40,7 @@ services:
6440
sysctls:
6541
net.core.somaxconn: 1024
6642
command: >
67-
redis-server
68-
--requirepass openIM123
69-
--appendonly yes
70-
--aof-use-rdb-preamble yes
71-
--save ""
43+
redis-server --requirepass openIM123 --appendonly yes --aof-use-rdb-preamble yes --save ""
7244
networks:
7345
- openim
7446

@@ -89,7 +61,6 @@ services:
8961
- ETCD_INITIAL_CLUSTER_TOKEN=tkn
9062
- ETCD_INITIAL_CLUSTER_STATE=new
9163
- ALLOW_NONE_AUTHENTICATION=no
92-
9364
## Optional: Enable etcd authentication by setting the following credentials
9465
# - ETCD_ROOT_USER=root
9566
# - ETCD_ROOT_PASSWORD=openIM123
@@ -212,7 +183,6 @@ services:
212183
KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP: "CONTROLLER:PLAINTEXT,EXTERNAL:PLAINTEXT,INTERNAL:PLAINTEXT"
213184
# Defines which listener is used for inter-broker communication within the Kafka cluster
214185
KAFKA_CFG_INTER_BROKER_LISTENER_NAME: "INTERNAL"
215-
216186
# Authentication configuration variables - comment out to disable auth
217187
# KAFKA_USERNAME: "openIM"
218188
# KAFKA_PASSWORD: "openIM123"

scripts/mongo-init.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/bash
2+
set -e
3+
4+
echo "Creating OpenIM user in database ${MONGO_INITDB_DATABASE}..."
5+
6+
mongosh -u "${MONGO_INITDB_ROOT_USERNAME}" -p "${MONGO_INITDB_ROOT_PASSWORD}" --authenticationDatabase admin <<EOF
7+
use ${MONGO_INITDB_DATABASE}
8+
if (!db.getUser("${MONGO_OPENIM_USERNAME}")) {
9+
db.createUser({
10+
user: "${MONGO_OPENIM_USERNAME}",
11+
pwd: "${MONGO_OPENIM_PASSWORD}",
12+
roles: [{role: "readWrite", db: "${MONGO_INITDB_DATABASE}"}]
13+
})
14+
print("OpenIM user created successfully")
15+
} else {
16+
print("User ${MONGO_OPENIM_USERNAME} already exists")
17+
}
18+
EOF
19+
20+
echo "OpenIM user setup completed"

0 commit comments

Comments
 (0)