0

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:

  1. 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 ???
  2. 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? )
  • To answer the latest "Why": The `node_modules` name you provided as host path to be mounted in the container is treated to be the name of a volume, cause it can't figure out it tends to be a path. You could fool it using `./node_modules` to make docker understand you mean the directory. – Ali Tou May 16 '20 at 00:35
  • And to answer your main question: I think your configurations are right and your problem is not related to MongoDB, but the Docker itself. Are you using Docker on Windows? There's a known issue about host mounts not working right after system startup in Windows: https://github.com/docker/for-win/issues/584#issuecomment-286792858 – Ali Tou May 16 '20 at 00:44

0 Answers0