Sequentially execution of docker services

Consider the following situation:

console-slow-tests:
  stage: test
  script:
    - {some other stuff}
    - "./gradlew console:checkSlow console:jacocoTestReportSlow"
    - {some other stuff}
  services:
    - name: docker.elastic.co/elasticsearch/elasticsearch:6.8.1
      alias: elasticsearch
    - name: {custom service}
      alias: server

In this scenario the job needs the custom service running. The custom service on the other hand needs elastic search running. If ES is not running custom service will fail shortly after startup.

According to the logs I see that the following happens:

  • elastic search and custom service is starting in parallel
  • custom service is up first, health check is OK, but som seconds after start custom service tries to connect to ES (a timed background job) which fails becaus ES is not yet running
  • threfore although health-check was OK custom service tears down again (this is by design of custom service)
  • elastic search is up and health check is OK
  • from git-labs point of view I have two both services up and running, which is not true
  • when starting the gradle test task it fails becaus custom service is down

Question 1:
Can you acknowledge this behavior? (Is it true that the services are start in parallel and only the script block waits for the health check of all defined services?)

Question 2:
How can I handle this situation?

I could create my own docker image with ES and custom service already running. But I feel that this is just one more image to mantain and this is therefore not my preferred solution.

I could change custom service so that it does not fail immediatly if ES is not running but waits for some time. Thats an option but it needs to change the current behavior of my service which is also not optimal.

Question 3
Can I handle this situation with gitlab-ci features (wait until service1 is started before service2 starts.

I am on gitlab.com