Docker mongodb 配置文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37063662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Docker mongodb config file
提问by ZuzEL
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.
有一种方法可以/data/db
将容器的目录链接到本地主机。但是我找不到有关配置的任何信息。如何/etc/mongo.conf
从我的本地文件系统链接到任何内容。或者也许使用了其他一些方法。请分享您的经验。
采纳答案by Thanh Nguyen Van
When you run docker container using this:
当您使用此运行 docker 容器时:
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.
/var/lib/mongo
是主机的 mongo 文件夹。
/data/db
is a folder in docker container.
/data/db
是 docker 容器中的一个文件夹。
回答by Argeo P.
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
我正在使用 mongodb 3.4 官方 docker 镜像。由于 mongod 默认不读取配置文件,这就是我启动 mongod 服务的方式
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
删除 -d 将显示容器的初始化
Using a docker-compose.yml:
使用 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'
回答by Vi.Ci
For some reason I should use MongoDb with VERSION:3.0.1
出于某种原因,我应该将 MongoDb 与 VERSION:3.0.1 一起使用
Now : 2016-09-13 17:42:06
现在 : 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 [ "" = '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:
我发现有两种方法可以启动 mongod 服务。我的尝试:
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 filemongod.conf
in it.
最后一个
-f
是 mongod 参数,你也可以--config
改用。确保类似的路径
your/host/dir
存在并且文件mongod.conf
在其中。
回答by Stunner
I merely wanted to know the command used to specify a config for mongo through the docker run
command.
我只是想知道用于通过docker run
命令为 mongo 指定配置的命令。
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/
并希望将其放置在/etc/
容器的文件夹中,您可以使用以下内容指定它:
-v /home/ubuntu/mongod.conf:/etc/mongod.conf
-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 指定命令以读取图像后的配置文件,如下所示:
mongo -f /etc/mongod.conf
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