Docker Community Forums

Share and learn in the Docker community.

Database population in image build vs at container run


(Enrique García Navalón) #1

Hi all,

I’m working in ‘dockerizing’ a complex web application into several containers. I have a mongodb database with the oficial mongo image that I want to be able to populate with some fixtures. I have two sets of fixtures, one for development and one for production (this fixture must be loaded in any new deployment). The way I see it, I have two options to implement this.

  • Use the base mongo image and using docker-compose figure out a way to run the fixture loading on each container creation.
  • Create a new image from the base mongo one and run the fixture loading to create a prepopulated image. Using a flag I could build for dev or prod images.

I can’t figure out which one is the “best” approach or more inline with docker philosfy when building apps. I have the feeling that the first one is better, but I would like more opinions on it and I can’t find any info online about it.

Thanks a lot for any feedback :slight_smile:

(Timgriffiths) #2

So i would tackle the problem like this, I would create a new layer on top of the public mongodb image and include all your fixtures, and then a script to import the prod ones, and a script to import the dev ones.

Then in the docker compose do something like this

  image: my-awesome-mongodb
  command: > 
      bash -lc '
        {(sleep 5; /scripts/ &}; 
  restart: always

This will sleep for 5 seconds, start the mongo db deamon, (hopefully after 5 seconds mongodb has started) then the script will fire to import your dev fixtures.

That’s how i would tackle this, be interested if someone has another method.

(Enrique García Navalón) #3

Cool! Happy to hear more ideas.

I was leaning toward something like that. I have wrapped the mongodb image with my own that includes the fixtures and the script, as you also propose. I use an environment variable to load dev or prod fixtures (the script is the same) and I was thinking of making docker-compose to load that env variable to the image. The idea of overriding the command in the docker file is pretty neat and what I was missing to avoid being force to load the fixtures every time I run if I don’t want to.

Interested in more opinions too! :slight_smile: