简体   繁体   English

将数据保留在mongodb docker容器的外部文件夹中

[英]Keep data in the external folder for mongodb docker container

I am using official docker container . 我正在使用官方docker容器 Here is a Dockerfile . 这是一个Dockerfile I am running container using this command: 我正在使用以下命令运行容器:

docker run --name mongo-db -d -p 27017:27017 -v  /mnt/lacie/databases/mongo/data:/data mongo

Next I connect to mongodb, create a db, a collection and insert data on it. 接下来,我连接到mongodb,创建一个db,一个集合并在其上插入数据。

I can stop the container docker stop , run it again docker start and all the data changes will be present in the database. 我可以停止容器docker stop ,再次运行docker start ,所有数据更改将出现在数据库中。

If I remove container docker rm and create new one - the data dissapear. 如果我删除容器docker rm并创建一个新容器-数据将消失。 Why? 为什么? And how can I fix it? 我该如何解决?

Based on the current Dockerfile you need to mount under /data/db not just /data . 根据当前的Dockerfile,您需要挂载在/data/db而不仅仅是/data Because of this mismatch your data is probably going into the container's private storage instead of into your mounted volume. 由于这种不匹配,您的数据可能会进入容器的私有存储中,而不是已装入的卷中。

Try debugging it like this: 尝试像这样调试它:

docker run --name mongo-db -d -p 27017:27017 -v /data mongo
docker stop mongo-db
docker run --name mongo-db2 -d -p 27017:27017 --volumes-from mongo-db mongo

If that works, then there's in an issue in the way mongo db is storing the data (I see in the Dockerfile for example that it specified /data/db as a VOLUME so it might expect the rest of the /data/ folder to be empty on initialization). 如果可行,则mongo db存储数据的方式存在问题(例如,我在Dockerfile中看到将/data/db指定为VOLUME因此可能希望/data/文件夹的其余部分为初始化时为空)。 That would probably be a bug but I'm just guessing without further info. 那可能是一个错误,但我只是在猜测而没有更多信息。

You can test my theory using: 您可以使用以下方法检验我的理论:

docker run --name mongo-db -d -p 27017:27017 mongo
docker stop mongo-db
docker run --name mongo-db2 -d -p 27017:27017 --volumes-from mongo-db mongo

I have the same problem. 我也有同样的问题。 the cause of the problem is not to use rm -f to remove the mongo container, we must to first command "stop" and then "rm" the mongo container. 问题的原因是不使用rm -f删除mongo容器,我们必须首先命令“停止”,然后“ rm” mongo容器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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