mongodb 如何设置docker mongo数据量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35400740/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:38:56  来源:igfitidea点击:

How to set docker mongo data volume

mongodbdocker

提问by Kery Hu

I want to use Dockerizing MongoDB and store data in local volume .

我想使用 Dockerizing MongoDB 并将数据存储在本地卷中。

But .. failed ...

但是……失败了……

It has mongo:latest images

它有 mongo:latest images

kerydeMacBook-Pro:~ hu$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
mongo               latest              b11eedbc330f        2 weeks ago         317.4 MB
ubuntu              latest              6cc0fc2a5ee3        3 weeks ago         187.9 MB

I want to store the mono data in ~/data . so ---

我想将单声道数据存储在 ~/data 中。所以 - -

kerydeMacBook-Pro:~ hu$ docker run -p 27017:27017 -v ~/data:/data/db --name mongo -d mongo
f570073fa3104a54a54f39dbbd900a7c9f74938e2e0f3f731ec8a3140a418c43

But ... it not work...

但是......它不起作用......

docker ps -- no daemon mongo

docker ps -- 没有守护进程 mongo

kerydeMacBook-Pro:~ hu$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

try to run "mongo" --failed

尝试运行“mongo”——失败

kerydeMacBook-Pro:~ hu$ docker exec -it f57 bash
Error response from daemon: Container f57 is not running

docker inspect mongo

码头工人检查mongo

kerydeMacBook-Pro:~ hu$ docker inspect mongo
[
{
    "Id": "f570073fa3104a54a54f39dbbd900a7c9f74938e2e0f3f731ec8a3140a418c43",
    "Created": "2016-02-15T02:19:01.617824401Z",
    "Path": "/entrypoint.sh",
    "Args": [
        "mongod"
    ],
    "State": {
        "Status": "exited",
        "Running": false,
        "Paused": false,
        "Restarting": false,
        "OOMKilled": false,
        "Dead": false,
        "Pid": 0,
        "ExitCode": 100,
        "Error": "",
        "StartedAt": "2016-02-15T02:19:01.74102535Z",
        "FinishedAt": "2016-02-15T02:19:01.806376434Z"
    },




"Mounts": [
        {
            "Source": "/Users/hushuming/data",
            "Destination": "/data/db",
            "Mode": "",
            "RW": true
        },
        {
            "Name": "365e687c4e42a510878179962bea3c7699b020c575812c6af5a1718eeaf7b57a",
            "Source": "/mnt/sda1/var/lib/docker/volumes/365e687c4e42a510878179962bea3c7699b020c575812c6af5a1718eeaf7b57a/_data",
            "Destination": "/data/configdb",
            "Driver": "local",
            "Mode": "",
            "RW": true
        }
    ],

if i do not set data volume ,mongo image can work !

如果我不设置数据量,mongo 图像可以工作!

But , when setting data volume ,it not ... Who can help me ?

但是,在设置数据量时,它不是...谁能帮帮我?

采纳答案by VonC

Try and check docker logs to see what was going on when the container stopped and go in "Existed" mode.

尝试检查docker 日志以查看当容器停止并进入“现有”模式时发生了什么。

See also if specifying the full path for the volume would help:

另请参阅指定卷的完整路径是否有帮助:

docker run -p 27017:27017 -v /home/<user>/data:/data/db  ...

The OP adds:

OP补充说:

docker logs mongo 
exception in initAndListen: 98 
Unable to create/open lock file: /data/db/mongod.lock 
errno:13 Permission denied 
Is a mongod instance already running?
terminating 2016-02-15T06:19:17.638+0000 
I CONTROL [initandlisten] dbexit: rc: 100 

An errno:13 is what issue 30is about.

errno:13 是问题 30的内容。

This commentadds:

此评论补充说:

It's a file ownership/permission issue (not related to this docker image), either using boot2docker with VB or a vagrant box with VB.

Nevertheless, I managed to hack the ownership, remounting the /Users shared volume inside boot2docker to uid 999 and gid 999 (which are what mongo docker image uses)and got it to start:

这是一个文件所有权/权限问题(与此 docker 映像无关),无论是在 VB 中使用 boot2docker 还是在 VB 中使用 vagrant box。

尽管如此,我还是设法破解了所有权,将 boot2docker 中的 /Users 共享卷重新挂载到 uid 999 和 gid 999(这是 mongo docker 映像使用的内容)并启动它:

$ boot2docker ssh
$ sudo umount /Users
$ sudo mount -t vboxsf -o uid=999,gid=999 Users /Users

But... mongod crashes due to filesystem type not being supported (mmap not working on vboxsf)

但是... mongod 由于不支持文件系统类型而崩溃(mmap 不适用于 vboxsf)

So the actual solution would be to try a DVC: Data Volume Container, because right now the mongodb docmentions:

所以实际的解决方案是尝试DVC: Data Volume Container,因为现在mongodb doc提到:

MongoDB requires a filesystem that supports fsync()on directories.
For example, HGFS and Virtual Box's shared folders do not support this operation.

MongoDB 需要一个支持fsync()目录的文件系统。
例如,HGFS 和 Virtual Box 的共享文件夹不支持此操作。

So:

所以:

the mounting to OSX will not work for MongoDB because of the way that virtualbox shared folders work.

由于 virtualbox 共享文件夹的工作方式,安装到 OSX 对 MongoDB 不起作用。



For a DVC (Data Volume Container), try docker volume create:

对于 DVC(数据卷容器),请尝试docker volume create

docker volume create mongodbdata

Then use it as:

然后将其用作:

docker run -p 27017:27017 -v mongodbdata:/data/db  ...    

And see if that works better.

看看是否效果更好。

As I mention in the comments:

正如我在评论中提到

A docker volume inspect mongodbdata(see docker volume inspect) will give you its path (that you can then backup if you need)

A docker volume inspect mongodbdata(see docker volume inspect) 会给你它的路径(如果你需要,你可以备份)

回答by Joshua Cook

As of summer 2017, a local volume is not considered best practice.

截至 2017 年夏季,本地卷不被视为最佳实践。

Per Docker Docs:

每个Docker 文档

Volumes are the preferred mechanism for persisting data generated by and used by Docker containers.

卷是持久化 Docker 容器生成和使用的数据的首选机制。

docker volume create mongodbdata
docker run -p 27017:27017 -v mongodbdata:/data/db mongo

回答by Richard

Via Docker Compose:

通过 Docker Compose:

version: '2'
    services:
      mongodb:
        image: mongo:latest
        volumes:
          - ./<your-local-path>:/data/db

/data/dbis the location of the data saved on the container.

/data/db是保存在容器上的数据的位置。

<your-local-path>is the location on your machine AKA the host machine where the actual database journal files will be saved.

<your-local-path>是您机器上的位置,也就是将保存实际数据库日志文件的主机。

回答by ofir_aghai

Found a link: VirtualBox Shared Folders are notsupported by mongodb.

找到一个链接:mongodb支持VirtualBox 共享文件夹。