I have set up a mongo container connected to a nodejs app.
First I do docker-compose up --build where I start mongo as a service without auth, i.e commented out.
Then I login to the mongo terminal with docker exec -it mongoContainerID /bin/bash
After which I add two users with :
db.createUser(
{
user: "abc",
pwd: "abcpwd",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)
and
db.createUser(
{
user: "abc2",
pwd: "abc2pwd",
roles: [ { role: "readWrite", db: "test" } ]
}
)
and then restart: with auth enabled. So everything works fine until machine restarts.
After every machine restart I have to repeat this process again of adding users, otherwise I get authentication failed error : UserNotFound: Could not find user
And after checking the db with compass I see that there's no previous data.
Which means that persistent storage doesn't work like It's supposed to.
Here's my docker compose file:
version: "3"
services:
app:
build: .
command: nodemon -L app.js
volumes:
- "./:/usr/src/app"
- "node_modules:/usr/src/app/node_modules"
ports:
- "9000:3000"
depends_on:
- "mongo"
mongo:
image: "mongo"
ports:
- "27019:27017"
env_file: .env
environment:
- MONGO_INITDB_ROOT_USERNAME=${MONGO_INITDB_ROOT_USERNAME}
- MONGO_INITDB_ROOT_PASSWORD=${MONGO_INITDB_ROOT_PASSWORD}
- MONGO_INITDB_DATABASE=${MONGO_INITDB_DATABASE}
volumes:
- /my/persistent/dir/mongo:/data/db
command: mongod --auth
# adminmongo:
# image: "mrvautin/adminmongo"
# ports:
# - "1234:1234"
volumes:
node_modules:
> My assessment:
The mongodb persistent storage is not working like it's supposed to.
Questions
I have two questions that I think I could be missing:
- I have not created a folder /my/persistent/dir/mongo as being used in the mongo volume command. Shouldn't it be automatically created or do I have to create it manually? If yes then what should be the path? (the directory in which docker-compose file is present)/my/persistent/dir/mongo ???
- At the end of docker-compose file under the Volume tag I only have
node_modules written. Should I also mention the path to mongo volume ?
(If I don't write node_module volume it gives an error, but not when I skip mongo volume folder.. Why? )