1

What's the best practices creating a mongodb container in docker with default data (collections with documents)?

I've found this other thread (How to create a Mongo Docker Image with default collections and data?) that's creates another db-path and mounts it as a new volume. But it doesn't seems right to me cause aren't you ending up with unused paths and mounted volumes?

Is there right/another way to do this?

Community
  • 1
  • 1
Staplerz
  • 85
  • 1
  • 10
  • What do you mean by unused path? Inside the docker you are using /data/db and mount there any local volume – Yerken Mar 06 '16 at 07:51
  • According to the other thread I was linking to. When I try to create collections to /data/db it won't show up when the container is running. That's because /data/db is mounted as a volume afterwards. Do you have any idea how to achieve this? – Staplerz Mar 06 '16 at 07:57
  • You should mount the volume when u create the container, so let's say you have existing data in your local machine at `/data/inject/`, so to inject that data into your mongo container you need to do something like this: `sudo docker run \ --name mongo \ -v /data/inject:/data/db \ -p 27017:27017 -d mongo:3.2.1` – Yerken Mar 06 '16 at 08:15
  • Yes that's a way to do it. But I'm writing an app that I want other developers to contribute to and thereby have a "finished" image to start working with. The app requires some default collections and test-data to run. Have I got this wrong or should I do it in another way? – Staplerz Mar 06 '16 at 08:26
  • 1
    there are few ways to go: 1. you may want to add the data via `Dockerfile` (with pre-imported official `mongo` image) 2. seed the database with the default data through the app. You may want to write the code to seed the database on app start, with linkage to the running mongo docker container. Personally I would go with option 2, it seems more natural to me leaves you with more flexibility (reusability for other apps) – Yerken Mar 06 '16 at 08:34
  • 2
    http://stackoverflow.com/a/33558598/6309 should be the best practice – VonC Mar 06 '16 at 08:35
  • Cool, will try out both to see which fits me best. Any recommendations to run the seed script only once at startup @Yerken? – Staplerz Mar 06 '16 at 09:02
  • depends which language you are using for your app? – Yerken Mar 06 '16 at 09:20
  • Sure, I forgot :) I use nodejs – Staplerz Mar 06 '16 at 09:22
  • 1
    It is pretty simple then, just create a seed.js file somewhere in your app, and `require` it somewhere after your db is initialised. Every time your app (re)starts the database will be populated with test data. You `seed.js` will be somewhat like this: `const Model = require('../model'); Model.find({test:true}).remove(function(){ Model.create({name:'testing', test:true}); ...etc }); ` – Yerken Mar 06 '16 at 09:30
  • Yeah that would work, thanks for the advice. I think I will try to create a shell script that first starts up all the containers via docker-compose. Then it connects to the mogodb container and executes the script(s) required for bootstrapping the app. Not portable to Windows but how does it sounds? – Staplerz Mar 06 '16 at 11:11
  • So here is what I ended up with http://pastebin.com/227ZPkXY What do you think? Any improvements? The mongo_create_insert.sh (along with the insert-scripts) are copied into the container via the Dockerifle at buildtime. – Staplerz Mar 06 '16 at 12:52

0 Answers0