Mongo: Where the is the mongod.conf file (configuration of mongo)?

Created on 24 Jan 2017  路  20Comments  路  Source: docker-library/mongo

So yea, yous don't state in the documentation anything about custom configuration files so I had to go digging:

RUN set -x \
    && apt-get update \
    && apt-get install -y \
        ${MONGO_PACKAGE}=$MONGO_VERSION \
        ${MONGO_PACKAGE}-server=$MONGO_VERSION \
        ${MONGO_PACKAGE}-shell=$MONGO_VERSION \
        ${MONGO_PACKAGE}-mongos=$MONGO_VERSION \
        ${MONGO_PACKAGE}-tools=$MONGO_VERSION \
    && rm -rf /var/lib/apt/lists/* \
    && rm -rf /var/lib/mongodb \
    && mv /etc/mongod.conf /etc/mongod.conf.orig

RUN mkdir -p /data/db /data/configdb \
    && chown -R mongodb:mongodb /data/db /data/configdb
VOLUME /data/db /data/configdb

I'm guessing the /data/configdb directory? I'm guessing putting a mongod.conf file is sufficient for it to be loaded when running a container?

https://docs.mongodb.com/manual/reference/configuration-options/#use-the-configuration-file states it should be in the /etc/mongod.conf file, but you guys seem to rename it?

Most helpful comment

By default, mongod does _not_ load any particular configuration file (and simply uses the built-in defaults for all options). See https://docs.mongodb.com/manual/reference/configuration-options/ for more details about using a configuration file.

For this image specifically, adding either --config /path/to/some/file.conf or -f /path/to/some/file.conf to the command executed should be sufficient for telling mongod to use a particular configuration file.

All 20 comments

By default, mongod does _not_ load any particular configuration file (and simply uses the built-in defaults for all options). See https://docs.mongodb.com/manual/reference/configuration-options/ for more details about using a configuration file.

For this image specifically, adding either --config /path/to/some/file.conf or -f /path/to/some/file.conf to the command executed should be sufficient for telling mongod to use a particular configuration file.

Thanks for the speedy response! Hmm I've tried the following:

/srv/dockervolume/mongo/mongod.conf is local (not inside the container).

docker run -d -v /srv/dockervolume/mongo/mongodata/db-tillvaxt:/data/db --name mongo-tillvaxt -h mongo mongo:latest --config /srv/dockervolume/mongo/mongod.conf

docker run -d -v /srv/dockervolume/mongo/mongodata/db-tillvaxt:/data/db --name mongo-tillvaxt -h mongo mongo:latest -f /srv/dockervolume/mongo/mongod.conf

docker run -d -v /srv/dockervolume/mongo/mongodata/db-tillvaxt:/data/db -v /srv/dockervolume/mongo/mongod.conf:/etc/mongod.conf --name mongo-tillvaxt -h mongo mongo:latest -f /etc/mongod.conf

Both exit immediately. I'm guessing the -f flag is the server path and not inside the container?

Nope, -f is definitely going to be relative to the container (mongod inside the container cannot know anything about the host path).

Is your bind mount actually working? Can you try running it again with -it --rm instead of -d and use bash as the command to check that your bind mount actually contains what you expect it to?

docker run -d -v /srv/dockervolume/mongo/mongodata/db-tillvaxt:/data/db -v /srv/dockervolume/mongo/mongod.conf:/etc/mongod.conf --name mongo-tillvaxt -h mongo mongo:latest is staying up :/ (without the -f flag). I'm running 3.4.

And the files are being mounted correctly.

root@mongo:/# ls /etc/mongod.conf 
/etc/mongod.conf

root@mongo:/# ls /data/db/
WiredTiger     WiredTiger.wt     collection-0-3757819925251140775.wt   collection-2-3757819925251140775.wt  collection-8-3757819925251140775.wt  index-1-7971477933856231242.wt   index-5-3757819925251140775.wt  journal        storage.bson
WiredTiger.lock    WiredTigerLAS.wt  collection-0-7971477933856231242.wt   collection-4-3757819925251140775.wt  diagnostic.data              index-11-3757819925251140775.wt  index-7-3757819925251140775.wt  mongod.lock
WiredTiger.turtle  _mdb_catalog.wt   collection-10-3757819925251140775.wt  collection-6-3757819925251140775.wt  index-1-3757819925251140775.wt       index-3-3757819925251140775.wt   index-9-3757819925251140775.wt  sizeStorer.wt
root@mongo:/# mongo
MongoDB shell version: 3.0.14
connecting to: test
2017-01-24T21:33:35.902+0000 W NETWORK  Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused
2017-01-24T21:33:35.907+0000 E QUERY    Error: couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed
    at connect (src/mongo/shell/mongo.js:179:14)
    at (connect):1:6 at src/mongo/shell/mongo.js:179
exception: connect failed

mongod.conf:

# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# Where and how to store data.
storage:
  dbPath: /var/lib/mongodb
  journal:
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

# network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1


#processManagement:

security:
    authorization: enabled

#operationProfiling:

#replication:

#sharding:

## Enterprise-Only Options:

#auditLog:

#snmp:

@basickarl I can confirm that mounting to /etc/mongod.conf has no effect in the behavior of the container until you actually change the COMMAND to something like the below (using docker-compose here):
command: ["mongod", "-f", "/etc/mongod.conf"]

I think you figured that out already. Using your mongod.conf will not work though unless you make the server bindIP=0.0.0.0 (listening in all interfaces).

Is there a setting to allow a loopback IP and custom configuration like authorization at the same time?

@nestoru in addition to a bind mount, you need to modify the container command to use -f /path/to/your/mongod.conf -- no modification of the image is necessary

Is there a setting to allow a loopback IP and custom configuration like authorization at the same time?

I'm not sure I understand the question so here is what I think you are asking about. As far as I know, when using --auth on mongod, you can only setup the initial users from localhost. So, for the docker container that would have to be done via docker exec or another container that is using the same network namespace.

@nestoru Does that answer your question?

@tianon That is exactly what I did as explained above. Thanks!

@yosifkit I was trying not to leave the mongod.conf file listener bound to all interfaces. Your conf file
will not work because it binds to 127.0.0.1 (the loopback address in the container). However I realize that binding to all IPs inside the container (0.0.0.0) is perfectly fine so this is a non issue at all.

@tianon and @yosifkit I think this ticket should be closed now, correct? The original question can be answered simply by "use as docker COMMAND ["mongod", "-f", "/etc/mongod.conf"] and mount the config file from host to container".

Indeed!

@nestoru When i config my docker-compose.yml use command: ["mongod", "-f", "/etc/mongod.conf"]
i got an error:

mongodb            | about to fork child process, waiting until server is ready for connections.
mongodb            | forked process: 13
mongodb            | ERROR: child process failed, exited with error number 1

Here is my docker-compose.yml config:

version: '2'
services:
  mongodb:
    image: mongo:latest
    container_name: mongodb
    command: ["mongod", "-f", "/etc/mongod.conf"]
    ports:
      - "29019:27017"
    volumes:
     - /etc/mongod.conf:/etc/mongod.conf
     - /var/lib/mongo:/data/db

Here is my /etc/mongod.conf content:

# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

# Where and how to store data.
storage:
  dbPath: /var/lib/mongo
  journal:
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:

# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /var/run/mongodb/mongod.pid  # location of pidfile

# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0  # Listen to local interface only, comment to listen on all interfaces.


security:
  authorization: enabled

#operationProfiling:

#replication:

#sharding:

## Enterprise-Only Options

#auditLog:

Can you help me?

Your config file has to be inside the container, so you either need to
build an image that copies it in or bind mount it at time.

@tianon Thanks for your reply. In my docker-compose.yml i have mounted the systemd mongod.conf to container with

volumes:
     - /etc/mongod.conf:/etc/mongod.conf

Is this correct ?

Yep, looks right.

You can't mount a file in docker-compose, only directories

@danmutblix you can mount a file in docker-compose.yml just fine -- the caveat is that it has to exist before mounting, and I've used this successfully literally today, so you'll have to be a lot more specific about what isn't working for you

Yeah, you're right.

@yvanwangl Your config seems fine except your volume, You can not mount a file and you must mount directory instead

So your docker-compose.yml config should be like the following:

version: '2'
services:
  mongodb:
    image: mongo:latest
    container_name: mongodb
    command: ["mongod", "-f", "/etc/mongo/mongod.conf"]
    ports:
      - "29019:27017"
    volumes:
     - /etc/mongo:/etc/mongo
     - /var/lib/mongo:/data/db

@yuseferi I get the same, have any solve?

These sorts of questions/requests would be more appropriately posted to the Docker Community Forums, the Docker Community Slack, or Stack Overflow.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Jaware picture Jaware  路  5Comments

thomasbeauvais picture thomasbeauvais  路  5Comments

jjelev picture jjelev  路  5Comments

abhinav2127 picture abhinav2127  路  5Comments

deployable picture deployable  路  3Comments