diff --git a/.env.example b/.env.example index 0cd1290..c243c74 100644 --- a/.env.example +++ b/.env.example @@ -1,4 +1,14 @@ #!/bin/sh + +# Acknowledgements: +# Acknowledgment required before using this docker-compose setup: +# - New users: This will be set to true by default. +# - Existing users: If you have a previous setup (e.g., using the Bitnami MongoDB image), you must manually set this to true after running the steps in the README.md +# Without this acknowledgment, the compose file will fail to run to avoid unintended changes. +# Example: When migrating from Bitnami MongoDB to the official MongoDB image, data loss or misconfigurations can occur +# if the acknowledgment is not explicitly provided. This ensures a safe transition for existing setups. +ACK_MONGODB_BITNAMI_MIGRATION=true + # Change these REG_TOKEN= DOMAIN=localhost diff --git a/.gitignore b/.gitignore index ce800b7..9b41fc1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /data/ node_modules .env +backup/ diff --git a/README.md b/README.md index 87b3031..28148d8 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,7 @@ You can login to Grafana at: http://grafana.localhost with the default credentia First, clone this repository: + ```bash git clone --depth 1 https://github.com/RocketChat/rocketchat-compose.git ``` @@ -126,3 +127,73 @@ podman compose \ -f compose.yml \ up -d ``` + +--- + +## Advanced Migrating to Oficial MongoDB + +Set `ACK_MONGODB_BITNAMI_MIGRATION=true` in your `.env` just after the backup is finished + +If you're updating from older MongoDB (Bitnami) to the official MongoDB container: + +### Step 1: Backup Your Data +1. Find your MongoDB container: + ```bash + docker ps + # or if you are using podman + podman ps + ``` +2. Create a backup inside the container: + ```bash + docker exec -it mongodump -o /tmp/backup + # or if you are using podman + podman exec -it mongodump -o /tmp/backup + ``` +3. Copy the backup to your computer: + ```bash + docker cp :/tmp/backup ./backup + # or if you are using podman + podman cp :/tmp/backup ./backup + ``` +4. Ack the migration process adding `ACK_MONGODB_BITNAMI_MIGRATION=true` to your `.env` file + +### Step 2: Update the Setup +1. Stop all services: + ```bash + docker compose -f compose.database.yml -f compose.monitoring.yml -f compose.traefik.yml -f compose.yml down + # or if you are using podman + podman compose -f compose.database.yml -f compose.monitoring.yml -f compose.traefik.yml -f compose.yml down + ``` +2. Update setup files: + ```bash + git pull origin main + ``` + +### Step 3: Restore Data +1. Start only MongoDB: + ```bash + docker compose -f compose.database.yml up -d + # or if you are using podman + podman compose -f compose.database.yml up -d + ``` +2. Copy your backup back into MongoDB: + ```bash + docker cp ./backup :/tmp/backup + # or if you are using podman + podman cp ./backup :/tmp/backup + ``` +3. Restore the backup: + ```bash + docker exec -it mongorestore /tmp/backup + # or if you are using podman + podman exec -it mongorestore /tmp/backup + ``` + +### Step 4: Restart Everything +1. Start all services again: + ```bash + docker compose -f compose.database.yml -f compose.monitoring.yml -f compose.traefik.yml -f compose.yml up -d + # or if you are using podman + podman compose -f compose.database.yml -f compose.monitoring.yml -f compose.traefik.yml -f compose.yml up -d + ``` + diff --git a/compose.database.yml b/compose.database.yml index 0687de9..a8ac2bb 100644 --- a/compose.database.yml +++ b/compose.database.yml @@ -24,19 +24,15 @@ services: - -varz - "http://nats:8222" mongodb: - image: docker.io/bitnamilegacy/mongodb:${MONGODB_VERSION:-6.0} + image: docker.io/mongo:${MONGODB_VERSION:-6.0} restart: always volumes: - - ${MONGODB_HOST_PATH:-mongodb_data}:/bitnami/mongodb:rw + - ${MONGODB_HOST_PATH:-oficial_mongodb_data}:/data/db:rw + command: ["--replSet", "${MONGODB_REPLICA_SET_NAME:-rs0}", "--bind_ip_all", "--port", "${MONGODB_PORT_NUMBER:-27017}"] environment: - MONGODB_REPLICA_SET_MODE: ${MONGODB_REPLICA_SET_MODE:-primary} - MONGODB_REPLICA_SET_NAME: ${MONGODB_REPLICA_SET_NAME:-rs0} - MONGODB_PORT_NUMBER: ${MONGODB_PORT_NUMBER:-27017} - MONGODB_INITIAL_PRIMARY_HOST: ${MONGODB_INITIAL_PRIMARY_HOST:-mongodb} - MONGODB_INITIAL_PRIMARY_PORT_NUMBER: ${MONGODB_INITIAL_PRIMARY_PORT_NUMBER:-27017} - MONGODB_ADVERTISED_HOSTNAME: ${MONGODB_ADVERTISED_HOSTNAME:-mongodb} - MONGODB_ENABLE_JOURNAL: ${MONGODB_ENABLE_JOURNAL:-true} - ALLOW_EMPTY_PASSWORD: ${ALLOW_EMPTY_PASSWORD:-yes} + MONGO_INITDB_DATABASE: ${MONGO_INITDB_DATABASE:-admin} + ACK_MONGODB_BITNAMI_MIGRATION: ${ACK_MONGODB_BITNAMI_MIGRATION:?Please refer to https://github.com/RocketChat/rocketchat-compose?tab=readme-ov-file#advanced-migrating-to-oficial-mongodb} + # healthcheck: # test: ["CMD", "mongosh", "--host", "mongodb", "--port", "${MONGODB_PORT_NUMBER:-27017}", "--eval", "db.adminCommand('ping')"] # interval: 30s @@ -47,6 +43,43 @@ services: - ${MONGODB_PORT_NUMBER:-27017} ports: - "${MONGODB_BIND_IP:-127.0.0.1}:${MONGODB_PORT_NUMBER:-27017}:${MONGODB_PORT_NUMBER:-27017}" + mongodb-init: + image: docker.io/mongo:${MONGODB_VERSION:-6.0} + restart: on-failure + command: + - bash + - -c + - | + # $$ is to use the bash $ or this will be interpreted by docker/podman and use variables from env + # for the MONGODB_PORT_NUMBER we actually want to use the one from the .env + echo "Waiting for MongoDB to be ready" + until mongosh --host mongodb --port ${MONGODB_PORT_NUMBER:-27017} --eval "db.adminCommand('ping')" >/dev/null 2>&1; do + echo "Waiting for MongoDB to be ready..." + sleep 5 + done + echo "MongoDB ready" + + REPLICA_STATUS="$(mongosh --host mongodb --port ${MONGODB_PORT_NUMBER:-27017} --eval "rs.status().ok" 2>/dev/null || echo "")" + if [ -z "$$REPLICA_STATUS" ]; then + echo "Initializing the replica set..." + mongosh --host mongodb --port ${MONGODB_PORT_NUMBER:-27017} --eval "rs.initiate({_id: '${MONGODB_REPLICA_SET_NAME:-rs0}', members: [{ _id: 0, host: 'mongodb:${MONGODB_PORT_NUMBER:-27017}' }]})" + else + echo "Replica set already initialized." + exit 0 + fi + + sleep 1 + + # Check if the replicaset is initialized + REPLICA_STATUS=$(mongosh --host mongodb --port ${MONGODB_PORT_NUMBER:-27017} --eval "rs.status().ok" 2>/dev/null || echo "") + if [ -z "$$REPLICA_STATUS" ]; then + echo "Replica set not initialized." + else + echo "Replica set already initialized." + fi + depends_on: + - mongodb + mongodb-exporter: image: docker.io/percona/mongodb_exporter:${MONGODB_EXPORTER_VERSION:-0.44.0} depends_on: @@ -58,4 +91,4 @@ services: - --collect-all - --compatible-mode volumes: - mongodb_data: {driver: local} + oficial_mongodb_data: {driver: local}