简体   繁体   中英

Docker mongodb config file

There is a way to link /data/db directory of the container to your localhost. But I can not find anything about configuration. How to link /etc/mongo.conf to anything from my local file system. Or maybe some other approach is used. Please share your experience.

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/mongo.conf'
    volumes:
        - '/home/sa/data/mongod.conf:/etc/mongod.conf'
        - '/home/sa/data/db:/data/db'
    ports:
        - '37017:27017'

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.

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

For some reason I should use MongoDb with VERSION:3.0.1

Now : 2016-09-13 17:42:06


That is what I found:

#first step: run mongo 3.0.1 without conf
docker run --name testmongo -p 27017:27017 -d mongo:3.0.1

#sec step: 
docker exec -it testmongo cat /entrypoint.sh                                                                                        

#!/bin/bash
set -e

if [ "${1:0:1}" = '-' ]; then
    set -- mongod "$@"
fi

if [ "$1" = 'mongod' ]; then
    chown -R mongodb /data/db

    numa='numactl --interleave=all'
    if $numa true &> /dev/null; then
        set -- $numa "$@"
    fi

    exec gosu mongodb "$@" 
fi

exec "$@"

I find that there are two ways to start a mongod service. What I try:

  docker run --name mongo -d -v your/host/dir:/container/dir mongo:3.0.1  -f /container/dir/mongod.conf
  • the last -f is the mongod parameter, you can also use --config instead.

  • make sure the path like your/host/dir exists and the file mongod.conf in it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM