Populate a database with existing data

In case you are running a composition step, it will be run by default from the repository working directory. This means that you should be able to do anything you do locally from your repository.

Postgres

Official repository of Docker postgres image

https://hub.docker.com/_/postgres/

  • Сommit database files into the source control repository
  • Сreate a docker image with the database contents

    YAML

        postgres:
          image: postgres:latest
          ports:
            - 5432
          volumes:
            - /my/own/datadir:/var/lib/postgresql/data
          environment:
            POSTGRES_USER: $POSTGRES_USER
            POSTGRES_PASSWORD: $POSTGRES_PASSWORD
            POSTGRES_DB: $POSTGRES_DB

The - /my/own/datadir:/data/db part of the command mounts the /my/own/datadir directory from the underlying host system as /data/db inside the container, where Postgres by default will write its data files.

Example of repository

Just head over to the example repository in Github and follow the instructions there.

To create the folder ./data locally you can just build and run this docker-compose.yml locally. Also when using specifically postgres, you have another option. You can put .sql files inside /docker-entrypoint-initdb.d folder and it will run them when the container starts. See more info about it under section How to extend this image https://hub.docker.com/_/postgres/

YAML

  unit_test:
    type: composition
    working_directory: ${{main_clone}}
    composition:
      version: '2'
      services:
        postgres:
          image: postgres:latest
          ports:
            - 5432
          volumes:
            - ./data:/var/lib/postgresql/data
          environment:
            POSTGRES_USER: $POSTGRES_USER
            POSTGRES_PASSWORD: $POSTGRES_PASSWORD
            POSTGRES_DB: $POSTGRES_DB
    composition_candidates:
      test:
        image: your/image
        links:
          - postgres
        command: bash -c '/dataset/test-script.sh'
        environment:
          - POSTGRES_USER=$POSTGRES_USER
          - POSTGRES_PASSWORD=$POSTGRES_PASSWORD
          - POSTGRES_DB=$POSTGRES_DB
          - POSTGRES_HOST=$POSTGRES_HOST
    composition_variables:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_DB=my_db
      - POSTGRES_HOST=postgres

Mongo

Official repository of Docker mongo image

https://hub.docker.com/_/mongo/

  • Сommit database files into the source control repository
  • Сreate a docker image with the database contents

    YAML

        mongo:
          image: mongo
          volumes:
            - /my/own/datadir:/data/db

The - /my/own/datadir:/data/db part of the command mounts the /my/own/datadir directory from the underlying host system as /data/db inside the container, where MongoDB by default will write its data files.

Example of repository

Just head over to the example repository in Github.

To create the folder ./data locally you can just build and run this docker-compose.yml local

YAML

 unit_test:
    type: composition
    working_directory: ${{build_step}}
    composition:
      version: '2'
      services:
        mongo:
          image: mongo
          volumes:
            - ./data:/data/db
    composition_candidates:
      test:
        image: your/image
        links:
          - mongo
        command: bash -c "/src/test-script.sh"
        environment:
          - MONGO_PORT=27017
          - MONGO_HOST=mongo
          - MONGO_DB=demo