最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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".

发布评论

评论列表(0)

  1. 暂无评论