-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinit-db-on-linux.sh
More file actions
61 lines (46 loc) · 1.79 KB
/
Copy pathinit-db-on-linux.sh
File metadata and controls
61 lines (46 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/bin/bash
# Run with "bash ./init-db-on-linux.sh"
set -e
TEMP_CONTAINER_NAME="mongo-init-temp"
CONTAINER_NAME="altzone_db_dev"
SERVICE_NAME="db_dev"
VOLUME_NAME="altzone_db_dev"
MONGO_PORT=27017
USERNAME="rootUser"
PASSWORD="superSecretPassword"
DATABASE="altzone_dev"
REPLICA_NAME="rs0"
REPLICA_HOST="localhost"
# Step 1: Start temp container
echo "1. Starting temporary Mongo container to set volume metadata up..."
docker run -d \
--name $TEMP_CONTAINER_NAME \
-e MONGO_INITDB_ROOT_USERNAME=$USERNAME \
-e MONGO_INITDB_ROOT_PASSWORD=$PASSWORD \
-e MONGO_INITDB_DATABASE=$DATABASE \
-v $VOLUME_NAME:/data/db \
-p $MONGO_PORT:27017 \
mongo:8.0.12-rc0-noble
# Step 2: Wait for Mongo to be ready
echo "2. Waiting for MongoDB to start..."
until docker exec $TEMP_CONTAINER_NAME mongosh --quiet --eval "db.adminCommand('ping')" > /dev/null 2>&1; do
sleep 1
done
echo "MongoDB volume is up. Removing the temporary container"
docker stop $TEMP_CONTAINER_NAME
docker rm $TEMP_CONTAINER_NAME
echo "3. Initiating replica set"
echo "Starting MongoDB container with docker compose"
docker compose up -d $SERVICE_NAME
echo "Waiting for MongoDB to be ready..."
until docker exec $CONTAINER_NAME mongosh --quiet -u $USERNAME -p $PASSWORD --authenticationDatabase admin --eval "db.adminCommand('ping')" > /dev/null 2>&1; do
sleep 1
done
echo "Initiating replica set for the container..."
docker exec $CONTAINER_NAME mongosh --quiet -u $USERNAME -p $PASSWORD --authenticationDatabase admin --eval \
"rs.initiate({ _id: '${REPLICA_NAME}', members: [{ _id: 0, host: '${REPLICA_HOST}:27017' }] })"
echo "Replica set initiated."
# Step 4: Stop and remove the container
echo "4. Stopping and removing the container"
docker compose down
echo "MongoDB container shut down. Volume is now ready for docker-compose up."