Initialize data on dockerized mongo

First create a docker volume

docker volume create --name mongostore

then create your mongo container

docker run -d --name mongo -v mongostore:/data/db mongo:latest

The -v switch here is responsible for mounting the volume mongostore at the /data/db location, which is where mongo saves its data. The volume is persistent (on the host). Even with no containers running you will see your mongostore volume listed by

docker volume ls

You can kill the container and create a new one (same line as above) and the new mongo container will pick up the state of the previous container.

Initializing the volume Mongo initializes a new database if none is present. This is responsible for creating the initial data in the mongostore. Let's say that you want to create a brand new environment using a pre-seeded database. The problem becomes how to transfer data from your local environment (for instance) to the volume before creating the mongo container. I'll list two cases.

  1. Local environment

    You're using either Docker for Mac/Windows or Docker Toolbox. In this case you can easily mount a local drive to a temporary container to initialize the volume. Eg:

    docker run --rm -v /Users/myname/work/mongodb:/incoming \
      -v mongostore:/data alpine:3.4 cp -rp /incoming/* /data
    

    This doesn't work for cloud storage. In that case you need to copy the files.

  2. Remote environment (AWS, GCP, Azure, ...)

    It's a good idea to tar/compress things up to speed the upload.

    tar czf mongodata.tar.gz /Users/myname/work/mongodb
    

    Then create a temporary container to untar and copy the files to the mongostore. the tail -f /dev/null just makes sure that the container doesn't exit.

    docker run -d --name temp -v mongostore:/data alpine:3.4 tail -f /dev/null
    

    Copy files to it

    docker cp mongodata.tar.gz temp:.
    

    Untar and move to the volume

    docker exec temp tar xzf mongodata.tar.gz && cp -rp mongodb/* /data
    

    Cleanup

    docker rm temp
    

You could also copy the files to the remote host and mounting from there but I tend to avoid interacting with the remote host at all.

Disclaimer. I'm writing this from memory (no testing).


A more self-contained approach:

  • create javascript files that initialize your database
  • create a derived MongoDB docker image that contains these files

There are many answers that use disposable containers or create volumes and link them, but this seems overly complicated. If you take a look at the mongo docker image's docker-entrypoint.sh, you see that line 206 executes /docker-entrypoint-initdb.d/*.js files on initialization using a syntax: mongo <db> <js-file>. If you create a derived MongoDB docker image that contains your seed data, you can:

  • have a single docker run command that stands up a mongo with seed data
  • have data is persisted through container stops and starts
  • reset that data with docker stop, rm, and run commands
  • easily deploy with runtime schedulers like k8s, mesos, swarm, rancher

This approach is especially well suited to:

  • POCs that just need some realistic data for display
  • CI/CD pipelines that need consistent data for black box testing
  • example deployments for product demos (sales engineers, product owners)

How to:

  1. Create and test your initialization scripts (grooming data as appropriate)
  2. Create a Dockerfile for your derived image that copies your init scripts

    FROM mongo:3.4
    COPY seed-data.js /docker-entrypoint-initdb.d/
    
  3. Build your docker image

    docker build -t mongo-sample-data:3.4 .
    
  4. Optionally, push your image to a docker registry for others to use

  5. Run your docker image

    docker run                               \
        --name mongo-sample-data             \
        -p 27017:27017                       \
        --restart=always                     \
        -e MONGO_INITDB_DATABASE=application \
        -d mongo-sample-data:3.4
    

By default, docker-entrypoint.sh will apply your scripts to the test db; the above run command env var MONGO_INITDB_DATABASE=application will apply these scripts to the application db instead. Alternatively, you could create and switch to different dbs in the js file.

I have a github repo that does just this - here are the relevant files.


with the latest release of mongo docker , something like this works for me.

FROM mongo
COPY dump /home/dump
COPY mongo_restore.sh /docker-entrypoint-initdb.d/

the mongo restore script looks like this.

#!/bin/bash
# Restore from dump
mongorestore --drop --gzip --db "<RESTORE_DB_NAME>" /home/dump

and you could build the image normally.

docker build -t <TAG> .

Tags:

Docker

Mongodb