diff --git a/Makefile b/Makefile index dafbdd0..8e7660b 100644 --- a/Makefile +++ b/Makefile @@ -15,8 +15,10 @@ OUTPUT = "file" OUTDIR = "/home/voje/workdir/test_out" DBADDR = "" -DB_ADM_USER = testuser -DB_ADM_PASS = testpass +DB_ADM_USER = testadmin +DB_ADM_PASS = testadminpass +DB_USR_USER = testuser +DB_USR_PASS = testuserpass export .PHONY: dev-env preflight diff --git a/dockerfiles/database/Dockerfile b/dockerfiles/database/Dockerfile new file mode 100644 index 0000000..d96e6a3 --- /dev/null +++ b/dockerfiles/database/Dockerfile @@ -0,0 +1,5 @@ +FROM mongo:latest + +WORKDIR / +COPY init_inside_container.sh /. +COPY create.js /. \ No newline at end of file diff --git a/dockerfiles/database/Makefile b/dockerfiles/database/Makefile index 231800f..3eaaf2a 100644 --- a/dockerfiles/database/Makefile +++ b/dockerfiles/database/Makefile @@ -4,20 +4,31 @@ # mongo admin -u root -p password --eval "db.getSiblingDB('vlDB').addUser('vluser', 'password')" -.PHONY: start_db - STACKNAME = dbstack -all: run_stack create_users +.PHONY: start_db FORCE + +all: build_mongo run_stack create_users + +info: + echo "[*] RUN THIS A FEW TIMES (THE STACK NEEDS TIME TO CLEAN UP AND TO SPIN UP THE NODES)" + +create.js: FORCE + +FORCE: + echo 'db.auth("$(DB_ADM_USER)", "$(DB_ADM_PASS)")' > create.js + echo 'use valdb' >> create.js + echo 'db.createUser({user: "$(DB_USR_USER)", pwd: "$(DB_USR_PASS)", roles: ["readWrite"]})' >> create.js + +build_mongo: create.js + docker build . -t my-mongo --no-cache + +clean_stack: + docker stack rm $(STACKNAME) run_stack: docker stack deploy --compose-file mongodb-stack.yml $(STACKNAME) -create.js: - echo -e 'db.auth("$(DB_ADM_USER)", "$(DB_ADM_PASS)")\n \ - use valdb\n \ - db.createUser({user: "kristjan", pwd: "password", roles: ["readWrite"]})' > create.js - create_users: create.js - docker exec $(shell ./get_container_name.sh) mongo admin < create.js + docker exec $(shell ./get_container_name.sh) /init_inside_container.sh # rm create.js diff --git a/dockerfiles/database/README.md b/dockerfiles/database/README.md new file mode 100644 index 0000000..fe5b0ed --- /dev/null +++ b/dockerfiles/database/README.md @@ -0,0 +1,3 @@ +# Some caveats +* Access mongo-express using `0.0.0.0:8087` instead of `localhost:...`. +* Run make from the root Makefile `make database`. You might need to run it several times (wait for conatiners to come up, etc.). diff --git a/dockerfiles/database/create.js b/dockerfiles/database/create.js index 907d726..2b12f7d 100644 --- a/dockerfiles/database/create.js +++ b/dockerfiles/database/create.js @@ -1,5 +1,3 @@ -db.auth("testuser", "testpass") - \ - use valdb - \ - db.createUser({user: "kristjan", pwd: "password", roles: ["readWrite"]}) +db.auth("testadmin", "testadminpass") +use valdb +db.createUser({user: "testuser", pwd: "testuserpass", roles: ["readWrite"]}) diff --git a/dockerfiles/database/get_container_name.sh b/dockerfiles/database/get_container_name.sh index 5cd6681..e12b914 100755 --- a/dockerfiles/database/get_container_name.sh +++ b/dockerfiles/database/get_container_name.sh @@ -1,2 +1,2 @@ #!/bin/bash -docker ps | grep mongo:latest | awk '{print $1}' +docker ps | grep my-mongo | awk '{print $1}' diff --git a/dockerfiles/database/init_inside_container.sh b/dockerfiles/database/init_inside_container.sh new file mode 100755 index 0000000..76c3398 --- /dev/null +++ b/dockerfiles/database/init_inside_container.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +mongo admin < /create.js diff --git a/dockerfiles/database/mongodb-stack.yml b/dockerfiles/database/mongodb-stack.yml index 4c218e6..c28db6a 100644 --- a/dockerfiles/database/mongodb-stack.yml +++ b/dockerfiles/database/mongodb-stack.yml @@ -2,8 +2,8 @@ version: '3.1' services: - mongo: - image: mongo + my-mongo: + image: my-mongo restart: always environment: MONGO_INITDB_ROOT_USERNAME: ${DB_ADM_USER} @@ -13,7 +13,8 @@ services: image: mongo-express restart: always ports: - - 8081:8081 + - 8087:8081 environment: ME_CONFIG_MONGODB_ADMINUSERNAME: ${DB_ADM_USER} ME_CONFIG_MONGODB_ADMINPASSWORD: ${DB_ADM_PASS} + ME_CONFIG_MONGODB_SERVER: my-mongo