9

I am configuring a MongoDB replica set using YAML syntax. However, I'd like to use MONGODB_HOME environment variable to point to the database:

storage:
  dbPath: "ENV['MONGODB_HOME']/data/db"

I've tried using %, $, etc, but without success. Is it possible to do so?

Just in case, I'm working under Windows 7 64 bit.

Best regards

Faliorn
  • 1,351
  • 3
  • 12
  • 24

4 Answers4

6

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.

Stennie
  • 63,885
  • 14
  • 149
  • 175
  • Is there a change in the last few years? I.e. whether it's now possible to do dynamic configuration using variables or not – asgs Jan 24 '19 at 10:28
  • 2
    @asgs As at MongoDB 4.0 there are no applicable changes in production releases of MongoDB. However, some improvements have recently landed in the 4.1 development series (which will culminate with the 4.2 production release). MongoDB 4.2 will support [Externally Sourced Values for Configuration Files](https://docs.mongodb.com/master/release-notes/4.2/#externally-sourced-values-for-configuration-files) using `__exec` and `__rest` expansion directives. The new directives are available for testing as of the 4.1.4 development release, but some of the usage documentation is still in progress. – Stennie Jan 25 '19 at 13:01
  • That sounds good. I left that project some time ago, but will mark the answer as the right one :) – Faliorn Feb 04 '19 at 10:24
1

Inspired from this answer.

With a mongod.conf like that :

systemLog:
  destination: file
  path: /mongo/db/mongodb.log
  logAppend: true
storage:
  dbPath: /mongo/db
net:
  bindIp: 127.0.0.1
  port: {PORT}

Then that in your Dockerfile :

FROM mongo
COPY mongod.conf /mongo/mongod.conf
CMD sed -e "s/{PORT}/$PORT/g" < /mongo/mongod.conf 1> /mongo/mongod.conf && mongod -f /mongo/mongod.conf
niomu
  • 9
  • 4
1

Add the command-line option --configExpand exec to the mongod command.

It is then possible to expand an environment variable by using __exec: and bash. Here are a few lines from the file mongod.conf file where I used this method.

processManagement:
  fork: true
  pidFilePath: 
    __exec: "bash -c 'echo -n $XDG_RUNTIME_DIR/mongodb/mongod.pid'"
    type: string

The Mongodb version on the computer is 4.4.5.

[user@laptop ~]$ mongod --version | grep version | tail -1
    "version": "4.4.5",
[user@laptop ~]$ 

Update: The computer is running Linux. I just realized that the question was about Windows 7. Maybe it's possible to do something similar on Windows 7. I guess you would then need to replace bash with something else.

Erik Sjölund
  • 10,690
  • 7
  • 46
  • 74
0

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.

Hemmels
  • 842
  • 2
  • 10
  • 15