Skip to content

Mounting external working directory with data directory #122

Closed
@onlybytes

Description

@onlybytes

Hi,
I have to add some default data into mongo db and build a image.
I followed this thread - #64 (comment) - and understood that data wont persist in /data/db. Hence created my own data directory /data/db2. Everything works fine, but i am not able to mount the directory on the host system to the directory visible inside the container.

docker run -d -v $PWD/testdb:/data/db2 --name my_mongo_db my_mongo_image results in error: STORAGE [initandlisten] exception in initAndListen: 98 Unable to create/open lock file: /data/db2/mongod.lock errno:13 Permission denied Is a mongod instance already running?, terminating

docker run -d --name my_mongo_db my_mongo_image works fine.

Well, docker run -d -v $PWD/testdb:/data/db --name my_mongo_db my_mongo_image works fine. Unfortunately data wont be persisted in /data/db.

To solve this issue, the suggestions on the web were:

  • disabling SELinux on host system
  • setting SELinux to permissive mode
  • starting the docker container with :Z option on internal directory - something like /data/db2:Z
  • changing security context of host system's directory by running the command: chcon -Rv --type=mongod_var_lib_t testdb

Unfortunately none worked.

Here is my docker file:

FROM mongo:3.2

RUN mkdir -p /data/db2 \
    && echo "dbpath = /data/db2" > /etc/mongodb.conf \
    && chown -R mongodb:mongodb /data/db2 \
    && chmod 755 /data/db2

COPY ./scripts /opt/scripts

RUN /opt/scripts/initdata.sh

VOLUME /data/db2

CMD ["mongod", "--auth", "--dbpath", "/data/db2", "--smallfiles"]

Here is initdata.sh:

#!/bin/bash

set -e

mongod --fork --logpath /var/log/mongodb.log --dbpath /data/db2 --smallfiles

sleep 5

DATA_JS=/opt/scripts/initdata.js
for f in $DATA_JS; do mongo admin $f; done

mongod --dbpath /data/db2 --shutdown

chown -R mongodb:mongodb /data/db2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions