The ultimate Docker image for populating your MongoDB database 🚀
Define MongoDB documents in JSON, JavaScript or even TypeScript file(s). Import them with an easy-to-use Docker image.
-
Follow the tutorial to define documents and collections to import.
-
Pull the latest stable version of Mongo Seeding image.
docker pull pkosiec/mongo-seeding:{versionNumber}
Check the version number in GitHub releases.
-
Run the image. Mount your directory with input data with
-v {source}:{destination}
parameter. Specify workspace which is a directory containing data import structure. Usually it will be the same directory:docker run --rm --network=host -v /absolute/path/to/data/:/absolute/path/to/data/ -w /absolute/path/to/data pkosiec/mongo-seeding
Sometimes, in import data files you would like to
import
orrequire
other dependencies, like helper functions defined somewhere else or external libraries. In order to resolve them properly in Docker container, you have to to mount the root directory of your project. As a working directory, define exact path for a directory that contains just the import data.docker run --rm --network=host -v /absolute/path/to/project/:/absolute/path/to/project -w /absolute/path/to/project/import-data/ pkosiec/mongo-seeding
-
Configure seeding with environmental variables. See the following example:
docker run --rm --network=host -v /absolute/path/to/examples/:/absolute/path/to/data/ -w /absolute/path/to/data/ -e DB_URI='mongodb://127.0.0.1:27017/mydbname' -e DROP_DATABASE=true pkosiec/mongo-seeding
See Configuration section for details.
The Docker image is basically a containerized CLI tool. Therefore, to configure the project, use environmental variables described in Environmental Variables section of the CLI tool. Specify them with -e {key}={value}
parameters.
As with every Docker container, you can override the entrypoint and the command.
For example, to prevent Mongo Seeding Docker container from exiting after seeding, use the following command:
docker run --rm --entrypoint="sh" pkosiec/mongo-seeding -c 'seed; sleep infinity'
You can prepare a customized Docker image for data import. It allows you to prepare image that contains import data inside and is already configured for your needs.
-
Prepare Dockerfile:
FROM pkosiec/mongo-seeding:latest # # Copy sample data # COPY ./data /import-data # # Set environmental variables (optional) # ENV DB_HOST 127.0.0.1 ENV DB_NAME mydbname ENV DB_PORT 27017 ENV DROP_DATABASE true ENV REPLACE_ID true ENV SET_TIMESTAMPS true # # Set default workspace to not specify it every time the image is ran # WORKDIR /import-data
If in any import data file there is an external dependency, copy not only data import files, but also
package.json
withpackage-lock.json
and runnpm install
during image build. -
Build the image:
docker build -t custom-mongo-seeding .
-
Run the image
docker run --rm --network="host" custom-mongo-seeding
See examples directory for an example of custom Docker image.