Skip to content

Mounting external working directory with data directory #122

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
onlybytes opened this issue Nov 13, 2016 · 6 comments
Closed

Mounting external working directory with data directory #122

onlybytes opened this issue Nov 13, 2016 · 6 comments

Comments

@onlybytes
Copy link

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
@ozlerhakan
Copy link
Contributor

ozlerhakan commented Nov 13, 2016

Hi @onlybytes ,

I am not sure why you use an initdata file at first glance but if you comment the following lines, your container should work fine:

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/init.sh
#VOLUME /data/db2

CMD ["mongod", "-f", "/etc/mongodb.conf", "--smallfiles"]

docker run --rm your-mongo-image


But if you use bind mount volume, your db path i.e /data/db2 will be owned by root not mongodb. You can again use -v to attach your host's dir with the target volume point: docker run --rm -v $PWD/testdb:/data/db2 your-mongo-image.

I hope , that can help your need.

@onlybytes
Copy link
Author

Thanks @ozlerhakan
The reason for using initdata file is to preload certain data in the image. Multiple teams can use the same image.

I will try without binding mount volume in Dockerfile.

Note: The host directory is mainly used for the storage purposes. For the reasons documented under "Where to Store Data" section of https://hub.docker.com/_/mongo/ page

@onlybytes
Copy link
Author

I think, this point from Docker documentation gives me the answer

Volumes are initialized when a container is created. If the container’s base image contains data at the specified mount point, that existing data is copied into the new volume upon volume initialization. (Note that this does not apply when mounting a host directory.)

So i can not do what i was doing..

If i need initial data then, i should let the Docker manage the storage of my database.

If i want to mount host directory, then i can not have initial data.

@tianon
Copy link
Member

tianon commented Nov 14, 2016 via email

@onlybytes
Copy link
Author

Thanks @tianon

@versatilepisces
Copy link

versatilepisces commented Oct 10, 2018

Hello @onlybytes @tianon @shykes ,

I followed the above steps however when I run my image, it says that the mongodb service is not running.

My Dockerfile:

FROM ubuntu:14.04
RUN groupadd -r mongodb && useradd -r -g mongodb mongodb &&
apt-get update &&
apt-get install -y mongodb=1:2.4.9-1ubuntu2 &&
apt-get install -y mongodb-clients &&
mkdir -p /data/db2 &&
echo "dbpath = /data/db2" > /etc/mongodb.conf &&
chown -R mongodb:mongodb /data/db2 &&
chmod 755 /data/db2

COPY initdata.sh /opt/scripts/initdata.sh
COPY accounts.json /opt/scripts/
RUN chmod +x /opt/scripts/initdata.sh
RUN /opt/scripts/initdata.sh
VOLUME /data/db2
CMD ["mongod", "--auth", "--dbpath", "/data/db2", "--smallfiles"]

My initdata.sh:
#!/bin/bash
set -e
mongod --fork --logpath /var/log/mongodb.log --dbpath /data/db2 --smallfiles
sleep 5
mongoimport --db test --collection branch --file /opt/scripts/accounts.json
mongod --dbpath /data/db2 --shutdown
chown -R mongodb:mongodb /data/db2

The log is like below:
Wed Oct 10 03:45:36.889 [initandlisten] MongoDB starting : pid=12 port=27017 dbpath=/data/db2 64-bit host=b032182e024e
Wed Oct 10 03:45:36.889 [initandlisten] db version v2.4.9
Wed Oct 10 03:45:36.889 [initandlisten] git version: nogitversion
Wed Oct 10 03:45:36.889 [initandlisten] build info: Linux orlo 3.2.0-58-generic #88-Ubuntu SMP Tue Dec 3 17:37:58 UTC 2013 x86_64 BOOST_LIB_VERSION=1_54
Wed Oct 10 03:45:36.889 [initandlisten] allocator: tcmalloc
Wed Oct 10 03:45:36.889 [initandlisten] options: { dbpath: "/data/db2", fork: true, logpath: "/var/log/mongodb.log", smallfiles: true }
Wed Oct 10 03:45:36.890 [initandlisten] journal dir=/data/db2/journal
Wed Oct 10 03:45:36.890 [initandlisten] recover : no journal files present, no recovery needed
Wed Oct 10 03:45:36.944 [FileAllocator] allocating new datafile /data/db2/local.ns, filling with zeroes...
Wed Oct 10 03:45:36.944 [FileAllocator] creating directory /data/db2/_tmp
Wed Oct 10 03:45:36.946 [FileAllocator] done allocating datafile /data/db2/local.ns, size: 16MB, took 0 secs
Wed Oct 10 03:45:36.947 [FileAllocator] allocating new datafile /data/db2/local.0, filling with zeroes...
Wed Oct 10 03:45:36.949 [FileAllocator] done allocating datafile /data/db2/local.0, size: 16MB, took 0.001 secs
Wed Oct 10 03:45:36.950 [websvr] admin web console waiting for connections on port 28017
Wed Oct 10 03:45:36.951 [initandlisten] waiting for connections on port 27017
Wed Oct 10 03:45:41.934 [initandlisten] connection accepted from 127.0.0.1:35640 #1 (1 connection now open)
Wed Oct 10 03:45:41.935 [FileAllocator] allocating new datafile /data/db2/test.ns, filling with zeroes...
Wed Oct 10 03:45:41.938 [FileAllocator] done allocating datafile /data/db2/test.ns, size: 16MB, took 0.003 secs
Wed Oct 10 03:45:41.939 [FileAllocator] allocating new datafile /data/db2/test.0, filling with zeroes...
Wed Oct 10 03:45:41.940 [FileAllocator] done allocating datafile /data/db2/test.0, size: 16MB, took 0.001 secs
Wed Oct 10 03:45:41.940 [FileAllocator] allocating new datafile /data/db2/test.1, filling with zeroes...
Wed Oct 10 03:45:41.942 [conn1] build index test.branch { _id: 1 }
Wed Oct 10 03:45:41.942 [conn1] build index done. scanned 0 total records. 0 secs
Wed Oct 10 03:45:41.944 [FileAllocator] done allocating datafile /data/db2/test.1, size: 32MB, took 0.001 secs
Wed Oct 10 03:45:41.950 [conn1] end connection 127.0.0.1:35640 (0 connections now open)
Wed Oct 10 03:45:41.964 [signalProcessingThread] got signal 15 (Terminated), will terminate after current cmd ends
Wed Oct 10 03:45:41.965 [signalProcessingThread] now exiting
Wed Oct 10 03:45:41.965 dbexit:
Wed Oct 10 03:45:41.965 [signalProcessingThread] shutdown: going to close listening sockets...
Wed Oct 10 03:45:41.965 [signalProcessingThread] closing listening socket: 9
Wed Oct 10 03:45:41.965 [signalProcessingThread] closing listening socket: 10
Wed Oct 10 03:45:41.965 [signalProcessingThread] closing listening socket: 11
Wed Oct 10 03:45:41.965 [signalProcessingThread] removing socket file: /tmp/mongodb-27017.sock
Wed Oct 10 03:45:41.965 [signalProcessingThread] shutdown: going to flush diaglog...
Wed Oct 10 03:45:41.965 [signalProcessingThread] shutdown: going to close sockets...
Wed Oct 10 03:45:41.965 [signalProcessingThread] shutdown: waiting for fs preallocator...
Wed Oct 10 03:45:41.965 [signalProcessingThread] shutdown: lock for final commit...
Wed Oct 10 03:45:41.965 [signalProcessingThread] shutdown: final commit...
Wed Oct 10 03:45:41.970 [signalProcessingThread] shutdown: closing all files...
Wed Oct 10 03:45:41.971 [signalProcessingThread] closeAllFiles() finished
Wed Oct 10 03:45:41.971 [signalProcessingThread] journalCleanup...
Wed Oct 10 03:45:41.971 [signalProcessingThread] removeJournalFiles
Wed Oct 10 03:45:41.972 [signalProcessingThread] shutdown: removing fs lock...
Wed Oct 10 03:45:41.972 dbexit: really exiting now

I am not sure how to start the service when I docker run with ports on the image.

Can you please help?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants