Docker mongodb config file

I merely wanted to know the command used to specify a config for mongo through the docker run command.

First you want to specify the volume flag with -v to map a file or directory from the host to the container. So if you had a config file located at /home/ubuntu/ and wanted to place it within the /etc/ folder of the container you would specify it with the following:

-v /home/ubuntu/mongod.conf:/etc/mongod.conf

Then specify the command for mongo to read the config file after the image like so:

mongo -f /etc/mongod.conf

If you put it all together, you'll get something like this:

docker run -d --net="host" --name mongo-host -v /home/ubuntu/mongod.conf:/etc/mongod.conf mongo -f /etc/mongod.conf

I'm using the mongodb 3.4 official docker image. Since the mongod doesn't read a config file by default, this is how I start the mongod service:

docker run -d --name mongodb-test -p 37017:27017 \
 -v /home/sa/data/mongod.conf:/etc/mongod.conf \
 -v /home/sa/data/db:/data/db mongo --config /etc/mongod.conf

removing -d will show you the initialization of the container

Using a docker-compose.yml:

version: '3'
services:
 mongodb_server:
    container_name: mongodb_server
    image: mongo:3.4
    env_file: './dev.env'
    command:
        - '--auth'
        - '-f'
        - '/etc/mongod.conf'
    volumes:
        - '/home/sa/data/mongod.conf:/etc/mongod.conf'
        - '/home/sa/data/db:/data/db'
    ports:
        - '37017:27017'

then

docker-compose up

When you run docker container using this:

docker run -d -v /var/lib/mongo:/data/db \ 
-v /home/user/mongo.conf:/etc/mongo.conf -p port:port image_name

/var/lib/mongo is a host's mongo folder.

/data/db is a folder in docker container.

Tags:

Docker

Mongodb