Skip to content

Latest commit

History

History

docker-image

Folders and files

NameName
Last commit message
Last commit date

parent directory

..

Mongo Seeding

Mongo Seeding Docker Image

Build Status

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.

Usage

  1. Follow the tutorial to define documents and collections to import.

  2. Pull the latest stable version of Mongo Seeding image.

    docker pull ghcr.io/pkosiec/mongo-seeding:{versionNumber}

    Check the version number in GitHub releases.

  3. 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 ghcr.io/pkosiec/mongo-seeding

    Sometimes, in import data files you would like to import or require 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/ ghcr.io/pkosiec/mongo-seeding
  4. 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 ghcr.io/pkosiec/mongo-seeding

    See Configuration section for details.

Configuration

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.

Override image entrypoint and command

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" ghcr.io/pkosiec/mongo-seeding -c 'seed; sleep infinity'

Docker image customization

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.

  1. Prepare Dockerfile:

    FROM ghcr.io/pkosiec/mongo-seeding
    
    #
    # 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 with package-lock.json and run npm install during image build.

  2. Build the image:

    docker build -t custom-mongo-seeding .
  3. Run the image

    docker run --rm --network="host" custom-mongo-seeding

See examples directory for an example of custom Docker image.