How to connect a gitlab-runner with a gitlab service via docker-compose?

Describe your question in as much detail as possible:

I have the following docker-compose file:

version: "3.8"

services:

  gitlab:
    image: gitlab/gitlab-ce:16.2.3-ce.0
    container_name: gitlab
    hostname: 'gitlab'
    environment:
      GITLAB_OMNIBUS_CONFIG: |
        external_url 'http://gitlab'
    ports:
      - '80:80'
      - '443:443'
      - '22:22'
    volumes:
      - gitlab_config:/etc/gitlab
      - gitlab_logs:/var/log/gitlab
      - gitlab_data:/var/opt/gitlab
    networks:
      - gitlab_net

  gitlab-runner:
    image: gitlab/gitlab-runner:v16.2.1
    container_name: gitlab-runner
    depends_on:
      gitlab:
        condition: service_healthy
    volumes:
      - '/var/run/docker.sock:/var/run/docker.sock'
      - gitlab_runner_config:/etc/gitlab-runner
    networks:
      - gitlab_net

volumes:
  gitlab_config:
  gitlab_logs:
  gitlab_data:
  gitlab_runner_config:

networks:
  gitlab_net:
    driver: bridge

I start it via ‘docker-compose up -d’

When everything is running properly I will create a gitlab-runner service by following the instructions from http://localhost/root/testproject/-/settings/ci_cd under the section of Runners, and creating a new runner via instructions from http://localhost/root/testproject/-/runners/new. After the runner is created I will register it with gitlab-runner register command from the gitlab-runner container with the http://gitlab as host and with the generated token.

After everything is done the runner is will start and when I run a new Pipeline I will get this error message:


Running with gitlab-runner 16.2.1 (674e0e29)
  on some_name_haha gWk8TaDTq, system ID: r_CjEBwP0lDBwE
Preparing the "docker" executor 00:03
Using Docker executor with image gcc ...
Pulling docker image gcc ...
Using docker image sha256:624c09f87a46c96ac13134027ece28486b7917dfed23cf90d96cae83521d7998 for gcc with digest gcc@sha256:11c592a2b05a47f15b2cee8013836e531edcc0de1f75b1827447902f1e948769 ...
Preparing environment 00:00
Running on runner-gwk8tadtq-project-1-concurrent-0 via 7166ed053422...
Getting source from Git repository 02:31
Fetching changes with git depth set to 20...
Initialized empty Git repository in /builds/root/testproject/.git/
Created fresh repository.
fatal: unable to access 'http://gitlab/root/testproject.git/': Could not resolve host: gitlab
ERROR: Job failed: exit code 1
  • Add the CI configuration from .gitlab-ci.yml and other configuration if relevant (e.g. docker-compose.yml)

image: gcc

stages:
  - build
  - test
  - deploy

build_job:
  stage: build
  script:
    - mkdir build
    - cd build
    - cmake ..
    - make
  artifacts:
    paths:
      - build/  # Assuming the executable or other build outputs are in this directory


test_job:
  stage: test
  script:
    - echo "Testing the project..."
    # Add test commands here

deploy_job:
  stage: deploy
  script:
    - echo "Deploying the project..."
    # Add deploy commands here

Can anyone help me to understand what exactly I am missing?

Hey,

I’m not sure which instructions you followed, but I believe you put the wrong URL when registering - instead of http://gitlab, you should put the whole instance URL, otherwise git inside the runner cannot properly resolve it. Have a look at official docs.

Hope this helps!

P.S. If you ever need a “dind” setup as well, you may want to have a look at this config.

I just read the documentation and also the link that you gave to me and am not sure that this is it. I’ll try the link you gave it to me a bit later, but regarding the URL when registering, what exactly would be there? Because so far I understand it should be the IP of the gitlab container itself, which because the docker containers can be accessed via their names instead of the IP it should be fine like this… or it isn’t?

In theory, you are correct. Docker internally resolves container IPs. However, I’m not sure if this IP gets properly to all gitlab runner components (there is also gitlab runner helper that does some cloning, uploading of artifacts, etc). Even if it does, it probably wouldn’t properly work with SSL & certificates (but we already see from your example it does not work).

So, on the link itself it says you should provide “GitLab Instance URL”. This is simply URL on which your GitLab installation is reachable, how you access it in the browser. So, for SaaS gitlab, it’s https://gitlab.com. For your internal one, you should know :smiley: It’s the same value you put in config as external_url.

ok so… I’ve just tried everything and I am out of ideas… Doesn’t matter what I’ve tried, it does not work whatever I try…

Things that I’ve try:

  1. adding the services as are presented here with and without for both with network_mode host and bridge
  2. trying to rename the gitlab container to be gitlab-container as the container name in order to be able to see the proper IP
  3. I’ve also tried to put the IP from the gitlab container directly there without the name (this is my case of 172.27.0.2 and it could not properly establish the connection with the runner.
  4. I’ve also tried to read this documentation but I did not find anything related to my problem sadly.

@paula.kokic any idea? :frowning:

Did you try re-registering the runner with proper GitLab URL instance as I described above?
You might need to remove some old generated data in config.toml of the runner as well.

Btw I’m not sure about your external_url in the GitLab config. Normally this should be proper DNS name over which you access GitLab in the browser. And normally with certificates (even self-signed), so https.