How to set docker mongo data volume

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)


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

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

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.

Tags:

Docker

Mongodb