How to run commands on the service containers in Gitlab CI?

Please see the below code snippet adapted from the Gitlab CI’s postgres service example project:

example:
  services:
    - postgres
  variables:
    # Configure postgres service (https://hub.docker.com/_/postgres/)
    POSTGRES_DB: custom_db
    POSTGRES_USER: custom_user
    POSTGRES_HOST_AUTH_METHOD: trust
  image: python
  script:
    # ...
    # Setup django application and do the migration
    # to the postgres database here ...
    # ...

    # The `postgres` service available makes 
    # a postgres container available to this job.
    # How to use that postgres container 
    # to run pg_dump here?
    - pg_dump ...

My usecase is as described in the comments above. I want to setup a django application, perform migration to the postgres database, then use pg_dump to export that database as an artifact.

I have read and understand from the documentation that The services feature ... does not, add any software from the defined services images to the job’s container..

I don’t want to add any software to the job container, I only want to use the software that is already present on the service container. For example, on my local machine, I can do the following:

docker run -it my-postgres-container-name pg_dump -U postgres ...

What is the equivalent approach to access the postgres service container that is linked to the job?

Gitlab CI version: gitlab-runner 16.3.0 (8ec04662)

anything used as “service” is a black box that should do whatever you need itself. If you need a service to do something that is not in it’s ENTRYPOINT or CMD you need to create your own image, customize it and use that.

1 Like

Thank you, @balonik. It sounds pretty strange that the container is running right there but it cannot be fully used in the workflow. Creating a custom image based on postgres seems like an overkill and a poor usability design by Gitlab CI.

This is not a criticism of your answer of course, but now I can rest peacefully that it is indeed not possible. I have given up on Gitlab ever taking usability seriously, so I will just make do with whatever workarounds that satisfy Gitlab CI.

If you are missing a feature you can look in Issues · GitLab.org / GitLab · GitLab if it’s already requested, maybe it’s being worked on already. If not there you can create a new one and it might get implemented in time.

1 Like