0

For a dev stack, I want to setup a docker-container of mongo via docker-compose. On its build, I want to initialize the database with some development data so that the stack works.

Yet according to the mongo's documentation, I can only seem to find a manual process:

Start the Database
$ docker run --name some-mongo -d mongo --auth
Add the Initial Admin User
$ docker exec -it some-mongo mongo admin
connecting to: admin
> db.createUser({ user: 'jsmith', pwd: 'some-initial-password', roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] });
Successfully added user: {
    "user" : "jsmith",
    "roles" : [
        {
            "role" : "userAdminAnyDatabase",
            "db" : "admin"
        }
    ]
}

I am looking for a way, to tell the image to run the query once and only once during the build in order to initialize the dev environment.

Basically, I am trying to achieve something similar to what mariadb image offers:

Initializing a fresh instance

When a container is started for the first time, a new database with the specified name will be created and initialized with the provided configuration variables. Furthermore, it will execute files with extensions .sh, .sql and .sql.gz that are found in /docker-entrypoint-initdb.d. Files will be executed in alphabetical order. You can easily populate your mariadb services by mounting a SQL dump into that directory and provide custom images with contributed data. SQL files will be imported by default to the database specified by the MYSQL_DATABASE variable.

Now I realize that docker isn't magic and that this is a feature of mariadb and not necessarily mongo, I still am looking to implement a similar idea and wonder how I can achieve that.

k0pernikus
  • 60,309
  • 67
  • 216
  • 347
  • If you just need some initial data, you can add the files to data directory: https://stackoverflow.com/questions/33558506/how-to-create-a-mongo-docker-image-with-default-collections-and-data – Alex Blex Feb 19 '18 at 11:38
  • I created an issue about that on the docker mongo repository: https://github.com/docker-library/mongo/issues/247 – k0pernikus Feb 19 '18 at 14:54

2 Answers2

0

I wrote a script, that starts my docker mongo container.

I use a named volume to store mongo's data - If that volume does not exist, it is created. In this case the following command is executed within the script to initialize the mongo container.

docker exec "$container_name" mongo admin --eval 'db.createUser({user: "admin", pwd: "password", roles:[{role:"userAdminAnyDatabase",db:"admin"}]});'

Until now, I found no better way to solve this.


Previously, I created the users manually and backed up the volume's data. When initializing the mongo container. I then restored that backup into the container on deployment.

I found this to consist of too many manual steps.

Update:

The tutum/mongodb image has a preset admin user and furthermore uses environment variables in order to set a user.

Actually, Bret Fisher is right - Doing this within the image is the best way.

rocksteady
  • 2,320
  • 5
  • 24
  • 40
0

The best way to handle this, like the official postgres, mysql, and mariadb databases do, is via a ENTRYPOINT shell script. Here's the latest one for mariadb.

You can find these in the images themselves, or in Hub by clicking Dockerfile links, then clicking the GitHub breadcrumbs for the pwd because the Hub official image standard is entrypoint scripts in the same GitHub directory has Dockerfiles usually.

The Entrypoint script starts on every container start, first, before the CMD is executed. The way Entrypoint actually works is a little more nuanced, but here's some info in the Docs.

In general, these entrypoint scripts do the same set of tasks for all the official db's, based on environment variables you set:

  1. Check if default db exists, if not, start daemon and create it
  2. Check if admin user exists, if not, create it
  3. Check if db user exists, if not, create it
  4. Shutdown daemon, and end script, which will kick off the Dockerfile CMD which is to start the db in foreground

You'll notice those official images have various envvars to customize all of those actions too.

You could start with one of those, then change the commands run to work for mongo. Advanced custom image usage might be pulling in sample data based on some envvar you create and set and adding sample data indexes too. This level of automation is great for teams developing on docker locally as they can just docker-compose up and the sample environment is setup fully for them, with no manual exec needed.

Bret Fisher
  • 8,164
  • 2
  • 31
  • 36