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 yourmariadb
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 theMYSQL_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.