Ver código fonte

chore(docker): mongodb users now automatically created

Signed-off-by: Jonathan <theflametrooper@gmail.com>
Jonathan 4 anos atrás
pai
commit
1caedef60c
5 arquivos alterados com 32 adições e 27 exclusões
  1. 6 0
      .env.example
  2. 1 0
      .gitignore
  3. 7 26
      README.md
  4. 10 1
      docker-compose.yml
  5. 8 0
      tools/setup-mongo.sh

+ 6 - 0
.env.template → .env.example

@@ -2,8 +2,14 @@ REDIS_PASSWORD=PASSWORD
 
 
 BACKEND_PORT=8080
 BACKEND_PORT=8080
 FRONTEND_PORT=80
 FRONTEND_PORT=80
+
 MONGO_PORT=27017
 MONGO_PORT=27017
+MONGO_ROOT_PASSWORD=PASSWORD_HERE
+MONGO_USER_USERNAME=musare
+MONGO_USER_PASSWORD=OTHER_PASSWORD_HERE
+
 MONGOCLIENT_PORT=3000
 MONGOCLIENT_PORT=3000
+
 REDIS_PORT=6379
 REDIS_PORT=6379
 
 
 COMPOSE_PROJECT_NAME=musare
 COMPOSE_PROJECT_NAME=musare

+ 1 - 0
.gitignore

@@ -8,6 +8,7 @@ Thumbs.db
 startRedis.cmd
 startRedis.cmd
 startMongo.cmd
 startMongo.cmd
 .database
 .database
+.db
 .redis
 .redis
 *.rdb
 *.rdb
 npm-debug.log
 npm-debug.log

+ 7 - 26
README.md

@@ -96,7 +96,7 @@ Now you have different paths here.
 
 
 _Configuration_
 _Configuration_
 
 
-To configure docker simply `cp .env.template .env` and configure the .env file to match your settings in `backend/config/default.json`.  
+To configure docker simply `cp .env.example .env` and configure the .env file to match your settings in `backend/config/default.json`.  
 The configurable ports will be how you access the services on your machine, or what ports you will need to specify in your nginx files when using proxy_pass. 
 The configurable ports will be how you access the services on your machine, or what ports you will need to specify in your nginx files when using proxy_pass. 
 `COMPOSE_PROJECT_NAME` should be a unique name for this installation, especially if you have multiple instances of Musare on the same machine.
 `COMPOSE_PROJECT_NAME` should be a unique name for this installation, especially if you have multiple instances of Musare on the same machine.
 `FRONTEND_MODE` should be either `dev` or `prod` (self-explanatory).
 `FRONTEND_MODE` should be either `dev` or `prod` (self-explanatory).
@@ -107,37 +107,18 @@ The configurable ports will be how you access the services on your machine, or w
 
 
 2. Set up the MongoDB database
 2. Set up the MongoDB database
 
 
-   1. Disable auth
+   1. Set the password for the admin/root user.
 
 
-      In `docker-compose.yml` remove `--auth` from the line `command: "--auth"` for mongo.
+      In `.env` set the environment variable of `MONGO_ROOT_PASSWORD`.
 
 
-   2. Start the database
+   2. Set the password for the musare user (the one the backend will use).
 
 
-      `docker-compose up mongo`
-
-   3. Connect to Mongo
-
-      `docker-compose exec mongo mongo admin`
-
-   4. Create an admin user
-
-      `db.createUser({user: 'admin', pwd: 'PASSWORD_HERE', roles: [{role: 'root', db: 'admin'}]})`
-
-   5. Connect to the Musare database
+      In `.env` set the environment variable of `MONGO_USER_USERNAME` and `MONGO_USER_PASSWORD`.
 
 
-      `use musare`
+   2. Start the database, which will generate the correct MongoDB users.
 
 
-   6. Create the musare user
-
-      `db.createUser({user: 'musare', pwd: 'OTHER_PASSWORD_HERE', roles: [{role: 'readWrite', db: 'musare'}]})`
-
-   7. Exit
-
-      `exit`
-
-   8. Add back authentication
+      `docker-compose up mongo`
 
 
-      In `docker-compose.yml` add back `--auth` on the line `command: ""` for mongo.
 
 
 3) Start the databases and tools in the background, as we usually don't need to monitor these for errors
 3) Start the databases and tools in the background, as we usually don't need to monitor these for errors
 
 

+ 10 - 1
docker-compose.yml

@@ -22,7 +22,16 @@ services:
     image: mongo:4.0
     image: mongo:4.0
     ports:
     ports:
     - "${MONGO_PORT}:27017"
     - "${MONGO_PORT}:27017"
-    command: "--auth"
+    environment:
+      - MONGO_INITDB_ROOT_USERNAME=admin
+      - MONGO_INITDB_ROOT_PASSWORD=${MONGO_ROOT_PASSWORD}
+      - MONGO_INITDB_DATABASE=musare
+      - MONGO_PORT=${MONGO_PORT}
+      - MONGO_ROOT_PASSWORD=${MONGO_ROOT_PASSWORD}
+      - MONGO_USER_USERNAME=${MONGO_USER_USERNAME}
+      - MONGO_USER_PASSWORD=${MONGO_USER_PASSWORD}
+    volumes:
+      - ./tools/setup-mongo.sh:/docker-entrypoint-initdb.d/setup-mongo.sh
   mongoclient:
   mongoclient:
     image: mongoclient/mongoclient
     image: mongoclient/mongoclient
     ports:
     ports:

+ 8 - 0
tools/setup-mongo.sh

@@ -0,0 +1,8 @@
+#!/bin/bash
+
+mongo musare \
+        --port ${MONGO_PORT} \
+        -u "admin" \
+        --authenticationDatabase "admin" \
+        -p ${MONGO_ROOT_PASSWORD} \
+        --eval "db.createUser({ user: '${MONGO_USER_USERNAME}', pwd: '${MONGO_USER_PASSWORD}', roles:[ { role:'readWrite', db: 'musare' } ] } );"