javascript - How to start mongo docker image with db user and password - Stack Overflow

i am trying to docker-ize my nodejsmongodbmongoose app.In my app.js i have the call to the local db:

i am trying to docker-ize my nodejs/mongodb/mongoose app. In my app.js i have the call to the local db:

mongoose.connect('mongodb://'mydbuser":"+mydbpassword"@"localhost"/"+nodekb,{ useMongoClient: true });

i am trying to docker-ize my nodejs/mongodb/mongoose app. In my app.js i have the call to the local db:

mongoose.connect('mongodb://'mydbuser":"+mydbpassword"@"localhost"/"+nodekb,{ useMongoClient: true });

My service part within th docker-pose.yml is looking like that:

  mongo:
    image: "mongo"
    ports:
      - "27017:27017"

I am using the default docker mongo image. My local mongo is using the mongo config file "mongod.cfg":

systemLog:
    destination: file
    path: "C:\\data\\log\\mongod.log"
storage:
    dbPath: "C:\\data"
net:
    port: 27017
security:
    authorization: enabled
setParameter:
    enableLocalhostAuthBypass: false

How can i inject the dbadmin, dbpw and the mongo config file so that it is used from the docker mongo image, wenn i do docker-pose up ? I guess with the COPY mand it is easy to inject the mongo config, but how can i tell the image to use it ?

Thank you all in advance.

Share Improve this question asked Oct 18, 2018 at 23:13 CodeThrowCodeThrow 352 silver badges5 bronze badges 3
  • 1 There are configuration examples with environment variables on the docker image page: hub.docker./_/mongo – maxm Commented Oct 18, 2018 at 23:18
  • You can pass environment variables for the username and password and dbname, and you can use a volume to set the config – maxm Commented Oct 18, 2018 at 23:18
  • Say Docker wasn't involved, but the MongoDB installation was too big to fit on your development system and you had to run it somewhere else. How would you configure that? – David Maze Commented Oct 18, 2018 at 23:20
Add a ment  | 

2 Answers 2

Reset to default 2

You can pass the related information under environment. It is clear on the docs.

Basic setup would be like below.

mongo:
    image: mongo
    restart: always
    ports:
      - 27017:27017
    environment: 
       MONGO_INITDB_ROOT_USERNAME: root
       MONGO_INITDB_ROOT_PASSWORD: example
  

Halil's answer is correct when you want to connect to DB "root".

But if you want to connect to DB "nodekb" rather than "root", you should first run "db.createUser()" mand(https://docs.mongodb./manual/reference/method/db.createUser/index.html) on DB "nodekb". Unfortunately, there is no way to setup this in "docker-pose.yml".

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745331463a4622897.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信