gansbrest / hodor

Small utility to streamline dev process with Docker on OS X (Boot2Docker/Docker Machine) and Linux
MIT License
154 stars 11 forks source link

How to specify volume share w/ Mac OSX host for `/data/db` directory of MongoDB container #4

Closed hironroy closed 10 years ago

hironroy commented 10 years ago

Thanks for the great utility. Everything is working as advertised. However, I can't figure out how to define a volume for the Mongo image data/db directory through the .hodorfile. Currently, the MongoDB data persists until the fileshare container is closed, but once fileshare is closed, the data is lost.

I'm running an Express.js app backended to MongoDB and would like to persist DB data beyond the life of the docker containers. Before using Hodor, I accomplished this with this command: docker run -d -p 27017:27017 -vpwd/tmp/data --name mongodb dockerfile/mongodb (after having configured Virtual Box Guest Additions).

After unsuccessfully trying a number of options in .hodorfile, I started trying to find a relevant option in the source. Beyond the root level container (the one specified in the task), there isn't currently a way to define a sync_to for a given container is there?

Here is my .hodorfile. I launch it by executing hodor npm start. Any help would be greatly appreciated:

containers:
  mongodb:
    background: true
    image: mongo
    ports:
      - 27017:27017
    onetime: false
  expressExample:
    background: false
    image: dockerfile/nodejs-bower-grunt
    ports: 
      - 3000:3000
    links:
      mongodb: mongodb
    workdir: "/Users/hironroy/coding/moo/node-express-mongoose-demo"
    onetime: true
tasks:
  npm:
    sync_project_to: /Users/hironroy/coding/moo/node-express-mongoose-demo
    cmd: "cd /Users/hironroy/coding/moo/node-express-mongoose-demo && npm"
    container: "expressExample"

Thanks!

gansbrest commented 10 years ago

@hironroy Thanks. Your Mongo data dir (data/db), is it inside of the project dir? Or it's located under some other path on your workstation?

If I understand your workflow correctly, you run hodor npm start when you need to work on the project, which runs expressExample container on the foreground and then when you done, you close this foreground process ( ctrl+c ) in terminal. Or maybe expressExample container just runs one command and terminates after that leaving mongo running?

hironroy commented 10 years ago

@gansbrest -- Thanks for the speedy reply. The idea is to have the MongoDB development data in the local project directory (ex. <project_dir>/tmp/data). That way, I have data to play with when developing.

The mongodb container is launched as detached (as a link dependency of the expressExample container). The npm start is as follows:

"start": "NODE_PATH=./config:./app/controllers NODE_ENV=development ./node_modules/.bin/nodemon server.js"

That launches an Express.js server that connects to MongoDB via Mongoose. As you said, I launch hodor npm start when I need to work on the project and (ctrl+c) in the terminal when done.

My question is: Using Hodor, what's the best way to hookup <project_dir>/tmp/data so that it's a volume that's mapped to the Mongo containers /data/db directory which is where the Mongo data is stored?

gansbrest commented 10 years ago

Yeah, I see your point. I'll try to come up with some good solution for that, for some reason 2 volumes under same path (project dir) gives me inconsistent results right now. Thanks for the report anyways.

gansbrest commented 10 years ago

@hironroy I added volumes option, which you can add to container definition in docker file. __PROJECT__ is the special required "label" showing that volumes only work for path within your project. General volumes under different paths are not supported due to the nature of the tool ( which should be commited to the repo and shared between devs )

volumes:
  __PROJECT__/conf: /data/conf

VERY IMPORTANT - before you test it, do docker rm -f fileshare ( remove fileshare ), then pull docker pull gansbrest/fs-base ( I updated the container ).

You can also remove your mongo container, to reinitialize volumes properly.

Let me know if it works for you as expected.

gansbrest commented 10 years ago

One more thing, every time you change volumes configuration of your container, you need to drop fileshare container ( docker rm -f fileshare ). In the future I may add volume change detection to avoid manual steps.