Populate a database with existing data - copied

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.

There are several ways to store data used by applications that run in Docker containers. We encourage users to familiarize themselves with the options available, including:

  • Create a data directory on the host system (outside the container) and mount this to a directory visible from inside the container. This places the database files in a known location on the host system, and makes it easy for tools and applications on the host system to access the files. The downside is that the user needs to make sure that the directory exists, and that e.g. directory permissions and other security mechanisms on the host system are set up correctly.

Postgres

Official repository of Docker postgres image

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

  • Create a data directory on a suitable volume on your host system, e.g. /my/own/datadir.
  • Add the following volumes to postgres container

    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.

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

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: ${{build_step}}
        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/

  • Create a data directory on a suitable volume on your host system, e.g. /my/own/datadir.
  • Add the following volumes to mongo container

    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 locally

YAML

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