From ee3279570131ab9ad7bc5ea0e1df0d967d1e5c3f Mon Sep 17 00:00:00 2001 From: stliang Date: Wed, 27 Dec 2023 21:33:56 -0800 Subject: [PATCH] fix docker-compose up failure due to mongo config error --- .gitignore | 3 +++ README.md | 1 + docker-compose.yml | 60 ++++++++++++++++++++++++++++++++++++---------- setup_mongo.sh | 54 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 106 insertions(+), 12 deletions(-) create mode 100755 setup_mongo.sh diff --git a/.gitignore b/.gitignore index c34ece8144..424300e840 100644 --- a/.gitignore +++ b/.gitignore @@ -58,3 +58,6 @@ target/ cover/ output.html + +# mongo keyfile +keyfile \ No newline at end of file diff --git a/README.md b/README.md index 11672501a5..09d55e34ac 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,7 @@ ## Installation ``` +openssl rand -base64 756 > keyfile docker-compose build docker-compose up ``` diff --git a/docker-compose.yml b/docker-compose.yml index ab9c7451a0..a91147facc 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,5 +1,50 @@ -version: '3' +--- +version: '3.8' + +volumes: + mongo1_data: + mongo1_config: + services: + mongo: + image: mongo:7.0 + container_name: mongo + restart: always + ports: + - "27017:27017" + environment: + MONGO_INITDB_ROOT_USERNAME: root + MONGO_INITDB_ROOT_PASSWORD: example + MONGO_INITDB_DATABASE: test + # MONGODB_ROOT_PASSWORD: example + # MONGODB_REPLICA_SET_MODE: primary + # MONGODB_REPLICA_SET_NAME: rs0 + # MONGODB_REPLICA_SET_KEY: replicaSetKey + entrypoint: + - bash + - -c + - | + cp /keyfile /mongo_keyfile + chmod 400 /mongo_keyfile + chown 999:999 /mongo_keyfile + exec docker-entrypoint.sh $$@ + command: "mongod --bind_ip_all --replSet rs0 --keyFile /mongo_keyfile" + + volumes: + - ./keyfile:/keyfile + - "mongo1_data:/data/db" + - "mongo1_config:/data/configdb" + + mongo-setup: + image: mongo:7.0 + container_name: mongo_setup + depends_on: + - mongo + restart: on-failure + entrypoint: ["/bin/bash", "/setup_mongo.sh"] + volumes: + - ./setup_mongo.sh:/setup_mongo.sh + api: build: context: . @@ -7,7 +52,8 @@ services: environment: SANDBOX_MODE: 'True' depends_on: - - mongo + mongo-setup: + condition: service_completed_successfully volumes: - "./src/openprocurement:/app/src/openprocurement:delegated" - "./docs:/app/docs:delegated" @@ -16,16 +62,6 @@ services: - "8000:80" command: ["gunicorn", "--bind", "0.0.0.0:80", "-k", "gevent", "--paste", "etc/service.ini", "--graceful-timeout=60", "--timeout=3600"] - mongo: - image: 'bitnami/mongodb:4.4.10' - environment: - MONGODB_ROOT_PASSWORD: example - MONGODB_REPLICA_SET_MODE: primary - MONGODB_REPLICA_SET_NAME: rs0 - MONGODB_REPLICA_SET_KEY: replicaSetKey - ports: - - 27017:27017 - # replica: # image: 'bitnami/mongodb:latest' # environment: diff --git a/setup_mongo.sh b/setup_mongo.sh new file mode 100755 index 0000000000..31c5bfba4d --- /dev/null +++ b/setup_mongo.sh @@ -0,0 +1,54 @@ +#!/bin/bash + +# TODO: This script assumes the following +# you named the container where your mongod runs 'mongo' +# you changed MONGO_INITDB_DATABASE to 'test' +# you set MONGO_INITDB_ROOT_USERNAME to 'root' +# you set MONGO_INITDB_ROOT_PASSWORD to 'example' +# you set the replica set name to 'rs0' (--replSet) +until mongosh --host mongo:27017 --eval 'quit(db.runCommand({ ping: 1 }).ok ? 0 : 2)' &>/dev/null; do + printf '.' + sleep 1 +done + +cd / +echo ' +try { + var config = { + "_id": "rs0", // TODO update this with your replica set name + "version": 1, + "members": [ + { + "_id": 0, + "host": "mongo:27017", // TODO rename this host + "priority": 2 + }, + ] + }; + rs.initiate(config, { force: true }); + rs.status(); + sleep(5000); + // creates another user + admin = db.getSiblingDB("admin"); + admin.createUser( + { + user: "otheradmin", + pwd: "othersecret", + roles: [ { role: "readWrite", db: "myowndb" }, + { role: "readWrite", db: "admin" } , + + ] + } + ); +} catch(e) { + rs.status().ok +} +' > /config-replica.js + + + +sleep 10 +# TODO update user, password, authenticationDatabase and host accordingly +mongosh -u root -p "example" --authenticationDatabase admin --host mongo:27017 /config-replica.js + +# if the output of the container mongo_setup exited with code 0, everything is probably okay