Services in concurrently running jobs using the same host

We have a problem in our GitLab pipeline with the database service of our build job. We use a self hosted docker runner. We build up a database service based on a docker image that’s sitting in our project registry.

maven-build:
  extends:
    - .cache
  stage: build
  services:
    - name: $CI_REGISTRY/postgres-unit-test:latest
      alias: postgres
  <rest of the job>
  only:
    - /^feature.*$/
    - /^bugfix.*$/
    - master

(I ommited some of the configuration)
It works flawlessly as long as only one build is running. When we allow the runner to run pipelines concurrently some of the builds fail. I think the databases are started correctly but because of the same host the tests sometimes use the one database and sometimes the other causing test failures.

I tried to use a more specific alias for the different commits like this:

services:
    - name: $CI_REGISTRY/postgres-unit-test:latest
      alias: postgres.$CI_COMMIT_SHORT_SHA

But as soon as the host contains a number the tests can’t connect to the database:

Unable to obtain connection from database (jdbc:postgresql://postgres.f3c6c84b:5432/db) for user ‘user’: The connection attempt failed.


SQL State : 08001

Error Code : 0

Message : The connection attempt failed.

Is there a way to use the services with concurrently running builds? We would like to speed up our build process. Waiting every time for one build to finish is adding up pretty fast as we’re a big team.

Thanks for taking the time to read this.

Kind regards,
Lukas

Hi Lukas, just saw this post. I like the idea of using an identifier that ought to be unique within the Runner context but I wonder if the host alias was usable as shown? what if you used just the SHORT_SHA with a “postgres_” prefix so it doesn’t look like a FQDN? I’m assuming your .gitlab-ci script is able to provide that hostname to the client trying to connect, based on the shown error message. I’ve currently got the pipelines using our db service capped at 1 job but once we increase that, same issue. With the same concerns because our db usage very much has to be exclusive per test. I hope you got it working for your needs.