83

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

But .. failed ...

It has 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 ---

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

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

try to run "mongo" --failed

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

docker inspect 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!

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

5 Answers 5

73

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

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:

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 30 is about.

This comment adds:

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:

$ 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)

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

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

So:

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


For a DVC (Data Volume Container), try 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)

12
  • run 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
    – Kery Hu
    Feb 15, 2016 at 6:21
  • 1
    Probably a permission issue then
    – VonC
    Feb 15, 2016 at 6:46
  • @KeryHu I have edited the answer to provide a possible workaround.
    – VonC
    Feb 15, 2016 at 7:07
  • @KeryHu I have edited the answer with the DVC (Data Volume Container) alternative, since you cannot mount a local folder to your container.
    – VonC
    Feb 15, 2016 at 7:16
  • @VonCYes,thanks your answer,i see it just now. but running boot2docker ssh bash: boot2docker: command not found ,
    – Kery Hu
    Feb 15, 2016 at 7:22
45

Per Docker Docs:

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

docker volume create mongodbdata
docker run -p 27017:27017 -v mongodbdata:/data/db mongo
4
  • 1
    I don't see this statement at docker docs anymore. Could you please review and update your answer?
    – heroin
    Jan 5, 2018 at 9:17
  • 4
    The new URL for this statement is docs.docker.com/storage/volumes Jun 3, 2019 at 2:19
  • Volumes are recommend: In addition, volumes are often a better choice than persisting data in a container’s writable layer, because a volume does not increase the size of the containers using it, and the volume’s contents exist outside the lifecycle of a given container. Source: docs.docker.com/storage/volumes
    – Kevin O.
    Dec 12, 2022 at 10:18
  • 1
    @KevinO. Are you confusing my use of the phrase "local volume" and "docker volume". As stated above, "volumes are the preferred mechanism". I think (though I have forgotten), the phrase "local volume" refers to an earlier approach before docker volumes existed. I'll updated the text of my response to not use the phrase "local volume". Dec 16, 2022 at 17:24
43

Via Docker Compose:

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

/data/db is the location of the data saved on the container.

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

1
8

For anyone running MongoDB container on Windows: as described here, there's an issue when you mount volume from Windows host to MongoDB container using path (we call this local volume).

You can overcome the issue using a Docker volume (volume managed by Docker):

docker volume create mongodata

Or using docker-compose as my preference:

version: "3.4"

services:
  ....
  
  db:
    image: mongo
    volumes:
      - mongodata:/data/db
    restart: unless-stopped
 
volumes:
  mongodata:

Tested on Windows 10 and it works

1
  • docker volume create mongodata
    – Sergei
    Oct 18, 2020 at 15:33
-5

Found a link: VirtualBox Shared Folders are not supported by mongodb.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.