Is it possible to add environment variables to MongoDB config file? Is it possible to add environment variables to MongoDB config file? mongodb mongodb

Is it possible to add environment variables to MongoDB config file?


The MongoDB config file (as at 3.0) only allows for static configuration values.

If you want to pass dynamic values you could invoke via the command line or a PowerShell script instead, eg:

mongod.exe --dbpath %HOME%\data\db

If you're planning on starting up multiple MongoDB server instances (for example, for different users) you'll also want to specify unique --port numbers to listen to.


Inspired from this answer.

With a mongod.conf like that :

systemLog:  destination: file  path: /mongo/db/mongodb.log  logAppend: truestorage:  dbPath: /mongo/dbnet:  bindIp: 127.0.0.1  port: {PORT}

Then that in your Dockerfile :

FROM mongoCOPY mongod.conf /mongo/mongod.confCMD sed -e "s/{PORT}/$PORT/g" < /mongo/mongod.conf 1> /mongo/mongod.conf && mongod -f /mongo/mongod.conf


I have had some success setting a location with

volumes:  - mongo:/data/db

As part of a docker-compose.yml, which means I don't have to use the --dbpath CL switch.