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 :

    user: "abc",
    pwd: "abcpwd",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]  


    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"  
    build: .  
    command: nodemon -L app.js  
      - "./:/usr/src/app"  
      - "node_modules:/usr/src/app/node_modules"  
      - "9000:3000"  
      - "mongo"  

    image: "mongo"
      - "27019:27017"  
    env_file: .env  
      - /my/persistent/dir/mongo:/data/db  
    command: mongod --auth  

  # adminmongo:  
  #         image: "mrvautin/adminmongo"  
  #         ports:  
  #             - "1234:1234"  

> My assessment:

The mongodb persistent storage is not working like it's supposed to.


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 at 0: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: github.com/docker/for-win/issues/584#issuecomment-286792858 – Ali Tou May 16 at 0:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.