chore: update docker-compose configuration to include health checks for MongoDB and add mongodb-init service for initialization scripts
This commit is contained in:
@@ -19,7 +19,10 @@ services:
|
|||||||
- NEXTAUTH_URL=${NEXTAUTH_URL}
|
- NEXTAUTH_URL=${NEXTAUTH_URL}
|
||||||
- AUTH_TRUST_HOST=true
|
- AUTH_TRUST_HOST=true
|
||||||
depends_on:
|
depends_on:
|
||||||
- mongodb
|
mongodb:
|
||||||
|
condition: service_healthy
|
||||||
|
mongodb-init:
|
||||||
|
condition: service_completed_successfully
|
||||||
networks:
|
networks:
|
||||||
- stripstream-network
|
- stripstream-network
|
||||||
deploy:
|
deploy:
|
||||||
@@ -55,13 +58,29 @@ services:
|
|||||||
memory: 512M
|
memory: 512M
|
||||||
ports:
|
ports:
|
||||||
- "27017:27017"
|
- "27017:27017"
|
||||||
command: ["mongod", "--auth", "--bind_ip_all", "--replSet", "rs0", "--keyFile", "/data/keyfile"]
|
command: ["mongod", "--replSet", "rs0", "--bind_ip_all", "--keyFile", "/data/keyfile"]
|
||||||
healthcheck:
|
healthcheck:
|
||||||
test: echo "try { rs.status() } catch (err) { rs.initiate({_id:'rs0',members:[{_id:0,host:'mongodb:27017'}]}) }" | mongosh -u ${MONGO_USER} -p ${MONGO_PASSWORD} --authenticationDatabase admin --quiet
|
test: mongosh --host localhost:27017 -u ${MONGO_USER} -p ${MONGO_PASSWORD} --authenticationDatabase admin --eval "db.adminCommand('ping')" --quiet || exit 1
|
||||||
interval: 10s
|
interval: 10s
|
||||||
timeout: 10s
|
timeout: 5s
|
||||||
retries: 5
|
retries: 5
|
||||||
start_period: 40s
|
start_period: 30s
|
||||||
|
|
||||||
|
mongodb-init:
|
||||||
|
image: mongo:latest
|
||||||
|
container_name: stripstream-mongodb-init
|
||||||
|
depends_on:
|
||||||
|
mongodb:
|
||||||
|
condition: service_healthy
|
||||||
|
environment:
|
||||||
|
MONGO_INITDB_ROOT_USERNAME: ${MONGO_USER}
|
||||||
|
MONGO_INITDB_ROOT_PASSWORD: ${MONGO_PASSWORD}
|
||||||
|
volumes:
|
||||||
|
- ./mongo-init-rs.sh:/mongo-init-rs.sh:ro
|
||||||
|
networks:
|
||||||
|
- stripstream-network
|
||||||
|
entrypoint: ["/bin/bash", "/mongo-init-rs.sh"]
|
||||||
|
restart: "no"
|
||||||
|
|
||||||
networks:
|
networks:
|
||||||
stripstream-network:
|
stripstream-network:
|
||||||
|
|||||||
27
mongo-init-rs.sh
Executable file
27
mongo-init-rs.sh
Executable file
@@ -0,0 +1,27 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
# Script d'initialisation du replica set MongoDB
|
||||||
|
|
||||||
|
echo "Attente du démarrage de MongoDB..."
|
||||||
|
sleep 10
|
||||||
|
|
||||||
|
echo "Initialisation du replica set..."
|
||||||
|
mongosh --host mongodb:27017 -u "$MONGO_INITDB_ROOT_USERNAME" -p "$MONGO_INITDB_ROOT_PASSWORD" --authenticationDatabase admin --eval "
|
||||||
|
try {
|
||||||
|
var status = rs.status();
|
||||||
|
print('Replica set déjà initialisé');
|
||||||
|
} catch (err) {
|
||||||
|
if (err.codeName === 'NotYetInitialized') {
|
||||||
|
rs.initiate({
|
||||||
|
_id: 'rs0',
|
||||||
|
members: [{ _id: 0, host: 'mongodb:27017' }]
|
||||||
|
});
|
||||||
|
print('Replica set initialisé avec succès');
|
||||||
|
} else {
|
||||||
|
print('Erreur: ' + err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"
|
||||||
|
|
||||||
|
echo "Vérification du statut du replica set..."
|
||||||
|
mongosh --host mongodb:27017 -u "$MONGO_INITDB_ROOT_USERNAME" -p "$MONGO_INITDB_ROOT_PASSWORD" --authenticationDatabase admin --eval "rs.status()"
|
||||||
|
|
||||||
Reference in New Issue
Block a user