MongoDB Docker Container Volume has no data

725 views
Skip to first unread message

CY Ong

unread,
Jan 1, 2018, 4:26:43 PM1/1/18
to mongodb-user

I downloaded a MongoDB docker image and wanted to mount a volume to persist data which was added into MongoDB. I am confused about whether the volume is mounted correctly and whether the container is using a volume or a bind mount.

First I created a volume:

docker volume create mongo-vol-1

Then I ran the docker run command:

docker run \
-p 30001:27017 \
--name mongo-1 \
--mount source=mongo-vol-1,target=/users/owner/documents/mongodb/data/\  
mongo mongod

The container could start and test data could be added successfully. However, after exiting the container, the /users/owner/documents/mongodb/data directory was still empty. 

I restarted the container and the test data could still be retrieved. 

Is the data stored in the container instead of the host directory? If so, how to link the host directory to where MongoDB stores the data?

Also, I ran a docker inspect command:

docker inspect mongo-1

The results were:

...
"Mounts": [
{
    "Type": "volume",
    "Source": "mongo-vol-1",
    "Target": "/users/owner/documents/mongodb/data"
}
...
"Volumes": {
     "/data/configdb": {},
     "/data/db": {}
        },
...

From the result above, I am not sure if the target directory is a mount or a volume?


This email is intended only for the named addressee(s) and may contain confidential and/or privileged information. If you are not the named addressee (or have received this e-mail in error), please notify the sender immediately. The unauthorised use, disclosure, distribution or copying of the contents in this e-mail is prohibited.

Thank you

Kevin Adistambha

unread,
Jan 17, 2018, 1:53:22 AM1/17/18
to mongodb-user

Hi

The target option of --mount describes the location inside the container, not on the host machine. See Use volumes page in the docker site for more details.

Since by default MongoDB stores its data in the /data/db directory, you need to specify the target to be /data/db. For example, let’s create a docker volume called mongo-vol-1:

$ sudo docker volume create mongo-vol-1
mongo-vol-1
$ sudo docker volume inspect mongo-vol-1
[
    {
        "CreatedAt": "2018-01-17T06:40:57Z",
        "Driver": "local",
        "Labels": {},
        "Mountpoint": "/var/lib/docker/volumes/mongo-vol-1/_data",
        "Name": "mongo-vol-1",
        "Options": {},
        "Scope": "local"
    }
]
$ sudo ls -l /var/lib/docker/volumes/mongo-vol-1/_data
total 0

The volume was created in the host under /var/lib/docker/volumes/mongo-vol-1/_data directory. You can see that intially this directory is empty.

Now we run the mongo docker image by specifying that the volume mongo-vol-1 should be mounted in the container as /data/db:

$ sudo docker run --detach -p 27017:27017 --name mongo-1 --mount source=mongo-vol-1,target=/data/db mongo

The directory now contains all the expected files:

$ sudo ls -l /var/lib/docker/volumes/mongo-vol-1/_data
total 60
-rw------- 1 vboxadd vboxsf 4096 Jan 17 06:41 collection-0--233989836741493852.wt
-rw------- 1 vboxadd vboxsf 4096 Jan 17 06:41 collection-2--233989836741493852.wt
drwx------ 2 vboxadd vboxsf 4096 Jan 17 06:41 diagnostic.data
-rw------- 1 vboxadd vboxsf 4096 Jan 17 06:41 index-1--233989836741493852.wt
-rw------- 1 vboxadd vboxsf 4096 Jan 17 06:41 index-3--233989836741493852.wt
drwx------ 2 vboxadd vboxsf 4096 Jan 17 06:41 journal
-rw------- 1 vboxadd vboxsf 4096 Jan 17 06:41 _mdb_catalog.wt
-rw------- 1 vboxadd vboxsf    2 Jan 17 06:41 mongod.lock
-rw------- 1 vboxadd vboxsf 4096 Jan 17 06:41 sizeStorer.wt
-rw------- 1 vboxadd vboxsf  114 Jan 17 06:41 storage.bson
-rw------- 1 vboxadd vboxsf   45 Jan 17 06:41 WiredTiger
-rw------- 1 vboxadd vboxsf 4096 Jan 17 06:41 WiredTigerLAS.wt
-rw------- 1 vboxadd vboxsf   21 Jan 17 06:41 WiredTiger.lock
-rw------- 1 vboxadd vboxsf  889 Jan 17 06:41 WiredTiger.turtle
-rw------- 1 vboxadd vboxsf 4096 Jan 17 06:41 WiredTiger.wt

Please note that the mongo image in the Docker Hub is not officially supported by MongoDB.

Best regards
Kevin

Reply all
Reply to author
Forward
0 new messages