Docker image for building MongoDB databases using the Mongo-Initializr
scripts.
By default the database is initialized on startup of the container using the Docker ENTRYPOINT which is executing the Mongo-Initializr script. The behavior of this script can be customized based on the MI_*
-variables.
MONGO_INITDB_ROOT_USERNAME
,MONGO_INITDB_ROOT_PASSWORD
: Username and password used for initializing and building the database (both default set to aerius).MONGO_INITDB_DATABASE
: The name of the database to initialize (default set to mongo).MI_INPUT_FILE
: The file which contains a list of dbdata-files and the corresponding database collection. If set the build and sync are executed.MI_NEXUS_BASE_URL
,MI_NEXUS_REPOSITORY
: The base-url and nexus repository where the dbdata-files are located.MI_DATABASE_VERSION
: The version of the database, which will be added to the database constants.MI_RUN_SCRIPT_FOLDER
: The folder there the_run.js
file is located. If not set, the runner wil not be executed.MI_DUMP_DATABASE
: Set totrue
if a (gzipped) binary export of the database needs to be created.MI_INITIALIZE_ON_BUILD
: Set totrue
if the database is going to be initialized during the Docker image build. You need to addRUN /usr/local/bin/docker-entrypoint.sh mongod
to youDockerfile
in order to start the initialization during the Docker build.MI_SKIP_DBDATA_SYNC
: Set totrue
if the dbdata sync should be skipped.MI_SKIP_UNSET_ENVS
: Set totrue
if all theMI_*
environment variables should be kept after thedocker-entrypoint.sh
entrypoint script is runned.MI_BIN_FOLDER_CLEANUP
: Set totrue
if the bin-folder should be removed.MI_SOURCE_FOLDER_CLEANUP
: Set totrue
if the source-folder should be removed.MI_DBDATA_FOLDER_CLEANUP
: Set totrue
if the dbdata-folder should be removed.HTTPS_DATA_USERNAME
,HTTPS_DATA_PASSWORD
: The username and password of the nexus repository used for syncing the dbdata-files.
MI_BIN_FOLDER
(/mi/bin
): Folder where you can find theMongo-Initializr
scripts.MI_SOURCE_FOLDER
(/mi/source
): Folder for the database source code.MI_DBDATA_FOLDER
(/mi/dbdata
): Folder for all dbdata files.MI_DUMP_FOLDER
(/mi/dump
): Folder for all database dumps. All dumps in this folder will be restored by the Mongo-Initializr script.
docker run \
--name example-project \
--volume /projects/example-project/git/example-project/source/:/mi/source \
--env MI_INPUT_FILE="/mi/source/example-project/src/data/initdb.json" \
--env MI_RUN_SCRIPT_FOLDER="/mi/source/example-project/src/main" \
--env MONGO_INITDB_DATABASE=example \
--env MI_DATABASE_VERSION=0.0.1 \
--env MI_NEXUS_BASE_URL=https://nexus.example-project.com \
--env MI_NEXUS_REPOSITORY=dbdata \
--env HTTPS_DATA_USERNAME=${HTTPS_DATA_USERNAME} \
--env HTTPS_DATA_PASSWORD=${HTTPS_DATA_PASSWORD} \
aerius-mongo-initializr:0.1-SNAPSHOT-7.0.6
docker run \
--name example-project \
--volume /projects/example-project/git/example-project/source/:/mi/source \
--volume /projects/example-project/dbdata/:/mi/dbdata \
--env MI_INPUT_FILE="/mi/source/example-project/src/data/initdb.json" \
--env MI_RUN_SCRIPT_FOLDER="/mi/source/example-project/src/main" \
--env MONGO_INITDB_DATABASE=example \
--env MI_DATABASE_VERSION=0.0.1 \
--env MI_SKIP_DBDATA_SYNC=true \
aerius-mongo-initializr:0.1-SNAPSHOT-7.0.6
Dockerfile containing all the information to be able to initialize the example-project database on startup of the container.
#syntax = docker/dockerfile:1
FROM aerius-mongo-initializr:0.1-SNAPSHOT-7.0.6
ENV MONGO_INITDB_DATABASE=example \
MONGO_INITDB_ROOT_USERNAME=example \
MONGO_INITDB_ROOT_PASSWORD=passwd \
MI_DATABASE_VERSION=0.01 \
MI_INPUT_FILE="/mi/source/example-project/src/data/initdb.json" \
MI_RUN_SCRIPT_FOLDER="/mi/source/example-project/src/main" \
MI_INITIALIZE_ON_BUILD=false \
MI_NEXUS_BASE_URL="https://nexus.example-project.com" \
MI_NEXUS_REPOSITORY=dbdata
# Copy all necessary scripts
COPY ./source /mi/source
docker build --tag example-project .
docker run \
--name example-project \
--volume ./data:/data/db \
--publish 27017:27017 \
--env HTTPS_DATA_USERNAME=user \
--env HTTPS_DATA_PASSWORD=passwd \
example-project:latest
Note: the Mongo docker-entrypoint will not initialize the database again if there is a known path in the dbPath folder. See the Mongo docker-entrypoint
.
#syntax = docker/dockerfile:1
FROM aerius-mongo-initializr:0.1-SNAPSHOT-7.0.6
ARG MONGO_INITDB_DATABASE=example-project \
MONGO_INITDB_ROOT_USERNAME=example \
MONGO_INITDB_ROOT_PASSWORD=passwd \
MI_DATABASE_VERSION=0.01 \
MI_INPUT_FILE="/mi/source/example-project/src/data/initdb.json" \
MI_RUN_SCRIPT_FOLDER="/mi/source/example-project/src/main" \
MI_INITIALIZE_ON_BUILD=true \
MI_NEXUS_BASE_URL="https://nexus.example-project.com" \
MI_NEXUS_REPOSITORY=dbdata \
HTTPS_DATA_USERNAME \
HTTPS_DATA_PASSWORD
# Copy all necessary scripts
COPY ./source /mi/source
# Run the docker-entrypoint in order to initialize the database
RUN /usr/local/bin/docker-entrypoint.sh mongod
There are two scripts for building the Docker images.
update.sh
- Creates the Dockerfile files for the specified Mongo versions.build_images.sh
- Builds all generated Dockerfile files.