Dealing with Docker Data volumes in MongoDB

If you tried to do what I said in my previous blog post, you’ll realize that the data volume does not get copied if you try to export  / import a mongoDB container.

In this instance, you’ll want to create a dedicated data container(https://docs.docker.com/userguide/dockervolumes/).  In fact for any data store you may want to use this pattern.  It will allow you to do updates and not lose your data.

// Here’s how I created a data container for mongoDB

docker run -d -v /data/db –name dbdata dockerfile/mongodb echo Data-only container for mongodb

// And then how I used it in my mongoDB container

docker run -d -p 27017:27017 –volumes-from dbdata –name db1 dockerfile/mongodb

You can test it by creating a collection in mongoDB, then stopping the container and removing it.  Then create another mongoDB container via the second command above and then look for the collection you  added previously.  You’ll see it. :)  I think you may be able to export the data container then import it on another machine.  

If you think about, it’s pretty cool.  The data container is separated from the implementation container.