I need support here to build my own mongo docker image.
I have a list of scripts to create and insert data into the MongoDB that shall be called in my Dockerfile to
According to the description of the image on DockerHub, there is a much cleaner and simpler solution for this.
When a container is started for the first time it will execute files with extensions .sh and .js that are found in /docker-entrypoint-initdb.d. Files will be executed in alphabetical order. .js files will be executed by mongo using the database specified by the MONGO_INITDB_DATABASE variable, if it is present, or test otherwise. You may also switch databases within the .js script.
First, the Dockerfile is as simple as
FROM mongo:4
COPY setup.sh /docker-entrypoint-initdb.d/
COPY scripts /
Then, in the setup.sh, add your user/collection creation script, for example
mongo=( mongo --host 127.0.0.1 --port 27017 --quiet )
mongo+=(
--username="$MONGO_INITDB_ROOT_USERNAME"
--password="$MONGO_INITDB_ROOT_PASSWORD"
--authenticationDatabase="$rootAuthDatabase"
)
CREATE_FILES=/scripts/*-create.js
for f in $CREATE_FILES; do "${mongo[@]}" "$MONGO_INITDB_DATABASE" $f; done
INSERT_FILES=/scripts/*-insert.js
for f in $INSERT_FILES; do "${mongo[@]}" "$MONGO_INITDB_DATABASE" $f; done