Running container in before_script of actual test?

I currently run unit tests for some projects using a PHP docker image. This works fine, with code self contained within the project.

services:
 - mysql:5.6
 - memcached:alpine

before_script:
  - composer install --no-progress --no-interaction
  - npm install
  - npm run dev
  - php artisan key:generate
  - php artisan migrate
  - php artisan db:seed

php7.1:
  image: giggsey/docker-php7:7.1-latest
  script:
    - vendor/bin/phpunit  --coverage-text --colors=never --coverage-clover coverage.xml

However, I’m trying to move away from having each project seed it’s own data, and instead have a separate docker image that automatically seeds all the data.

Is there a way of running this other docker container in the before_script section of the above test, having it using the existing services?

I managed to get something similar working in another project by mixing docker-compose, and running GitLab CI using the docker and dind services. However, this introduces a fair bit of complexity into the project that I’d like to avoid.

I’m happy to add additional packages etc. to my docker-php images (they’re already huge, and only used by me) if this would get it working.

Worked out how.

By using the ‘docker:dind’ service, I’m able to run docker commands.

The difficulty was passing the mysql service to the running container.

variables:
TIMEZONE: “Europe/London”
MYSQL_ROOT_PASSWORD: “root”
COMPOSER_DISCARD_CHANGES: “true”
DOCKER_DRIVER: overlay2
DOCKER_HOST: tcp://docker:2375

image: giggsey/docker-php7:7.1-latest

services:

  • mysql:5.6
  • memcached:alpine
  • docker:dind

before_script:

php7.2:
image: giggsey/docker-php7:7.2-latest
tags:
- docker
script:
- vendor/bin/phpunit --coverage-text --colors=never --coverage-clover coverage.xml

1 Like