Docker cannot start MongoDb with attached volume through data-only container

I’m trying to run a docker-compose on my Windows machine spinning up a MongoDB instance and a data-only container which proxies an attached volume containing the database files.

mongodata:  
  image: mongo:2.6.8
  volumes:
    - ./data/db:/data/db
  command: --break-mongo
mongo:  
  image: mongo:2.6.8
  volumes_from:
    - mongodata
  ports:
    - "27017:27017"
  command: --smallfiles --rest

*p.s. the –break-mongo command is there on purpose as it just needs to create the volume

  • Run process as Daemon on AWS Infrastructure
  • Docker capture packets
  • cannot pm2 list in docker containers
  • Error loading psycopg2 module when installed from a 'wheel'
  • Which java 8 docker image contains jmap?
  • How to run a cron job inside a docker container?
  • To my understanding, using a data-only volume pattern would handle permission issues but I can see the following error during the Mongo container startup:

     [0m2016-01-26T00:23:52.340+0000 [initandlisten] info preallocateIsFaster couldn't run due to: couldn't open file /data/db/journal/tempLatencyTest for writing errno:1 Operation not permitted; returning false
     [0m2016-01-26T00:23:52.341+0000 [initandlisten] Unable to remove temporary file due to: boost::filesystem::remove: Text file busy: "/data/db/journal/tempLatencyTest"
     [0m2016-01-26T00:23:52.344+0000 [initandlisten] exception in initAndListen: 13516 couldn't open file /data/db/journal/j._0 for writing errno:1 Operation not permitted, terminating
    

    Therefore I’m unable to use MongoDb with an attached volume from my local machine. Is there any way around this issue?

  • Forwarding traffic between two interfaces
  • Build docker in ASP.NET Core: “no such file or directory” error
  • Controlling where Docker starts incremental builds (use case: git clone inside Dockerfile)
  • change docker instance run arguments
  • Facing FileNotFoundException while accessing JSON File in classpath using java in docker containers(SprintBootApplication)
  • starting docker container from python messes up terminal settings
  • 3 Solutions collect form web for “Docker cannot start MongoDb with attached volume through data-only container”

    The documentation states

    If you are using Docker Machine on Mac or Windows, your Docker daemon
    has only limited access to your OS X or Windows filesystem. Docker
    Machine tries to auto-share your /Users (OS X) or C:\Users (Windows)
    directory. So, you can mount files or directories on OS X using.

    docker run -v /Users/<path>:/<container path> ...

    On Windows, mount directories using:

    docker run -v /c/Users/<path>:/<container path> ...

    All other paths come from your virtual machine’s filesystem. For example, if you are
    using VirtualBox some other folder available for sharing, you need to
    do additional work. In the case of VirtualBox you need to make the
    host folder available as a shared folder in VirtualBox
    . Then, you can
    mount it using the Docker -v flag.

    Basically, either try to give a full path beginning from your C:\Users folder as shown above, or if you can’t have that, make the host folder a shared folder in Virtualbox.

    Update

    No need to give a full path. docker-compose will handle that. You have to make sure that your docker-compose.yml is inside (somewhere down the line) of your Users folder. It can’t be in some root folder. If you are already doing that, then you will have to adjust your permissions. Just give full permissions to that folder.

    Update: Check out the latest Docker for Windows and MacOS X.

    Faster and more reliable: no more VirtualBox! The Docker engine is
    running in an Alpine Linux distribution on top of an xhyve Virtual
    Machine on Mac OS X or on a Hyper-V VM on Windows, and that VM is
    managed by the Docker application. You don’t need docker-machine to
    run Docker for Mac and Windows.

    Note: if Windows, you need Windows 10 Pro to make it work as Hyper-V is not included in other releases.

    For Docker Toolbox previously, it seems there is no solution at all on Windows and OS X due to VirtualBox. The image documentation indeed states:

    WARNING (Windows & OS X): The default Docker setup on Windows and OS X
    uses a VirtualBox VM to host the Docker daemon. Unfortunately, the
    mechanism VirtualBox uses to share folders between the host system and
    the Docker container is not compatible with the memory mapped files
    used by MongoDB (see vbox bug, docs.mongodb.org and related
    jira.mongodb.org bug). This means that it is not possible to run a
    MongoDB container with the data directory mapped to the host

    As an workaround I just copy from a folder before mongo deamon starts. Also, in my case I don’t care of journal files, so i only copy database files.

    I’ve used this command on my docker-compose.yml

    command: bash -c "(rm /data/db/*.lock && cd /prev && cp *.* /data/db) && mongod"

    And everytime before stoping the container I use:

    docker exec <container_name> bash -c 'cd /data/db && cp $(ls *.* | grep -v *.lock) /prev'

    Note: /prev is set as a volume. path/to/your/prev:/prev

    Another workaround is to use mongodump and mongorestore.

    • in docker-compose.yml: command: bash -c "(sleep 30; mongorestore
      --quiet) & mongod"
    • in terminal: docker exec <container_name> mongodump

    Note: I use sleep because I want to make sure that mongo started, and it takes a while.

    I know this involves manual work etc, but I am happy that at least I got mongo with existing data running on my Windows 10 machine, and still can work on my Macbook when I want.

    (croscopy https://stackoverflow.com/a/42044756/1894856)

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.