How to setup both GitLab and Runner on local machine correctly?

Job can’t access localhost hosted git repo

I just started to explore GitLab as Administrator (previously used it as developer only) to figure out how to setup gitlab with runners. To accomplish this, I decided to setup 2 containers on my local machine (GitLab Instance and Runner) via docker compose and it worked fine until I tried to run my job.

Problem: job (runner container) can’t see localhost hosted git repo
Job error:

Docker compose YAML:

version: '3.6'
services:
  instance:
    image: 'gitlab/gitlab-ee:latest'
    restart: always
    networks:
      - gitlab-network
    environment:
      GITLAB_OMNIBUS_CONFIG: |
        external_url 'http://localhost:8929'
        gitlab_rails['gitlab_shell_ssh_port'] = 2224
    ports:
      - '8929:8929'
      - '2224:22'
    volumes:
      - '$GITLAB_HOME/config:/etc/gitlab'
      - '$GITLAB_HOME/logs:/var/log/gitlab'
      - '$GITLAB_HOME/data:/var/opt/gitlab'
    shm_size: '256m'

  runner:
    image: 'gitlab/gitlab-runner:latest'
    restart: always
    networks:
      - gitlab-network
    volumes:
      - '/srv/gitlab-runner/config:/etc/gitlab-runner'
      - '/var/run/docker.sock:/var/run/docker.sock'
    command:
      - 'run'
      - '--user=gitlab-runner'
      - '--working-directory=/home/gitlab-runner'

networks:
  gitlab-network:
    driver: bridge

I can access repo from runner container using docker bridge name:

http://instance:8929/root/runner_test.git/

But I don’t know how to make runner container see it as:

http://localhost:8929/root/runner_test.git/

I found similar problem with unclear self-answer using extra_hosts field in config, so I tried different variations of extra_hosts = ["localhost:instance"], but noting worked so I’m here to ask for help.

There is config.toml of my runner:

concurrent = 1
check_interval = 0
shutdown_timeout = 0

[session_server]
  session_timeout = 1800

[[runners]]
  name = "style_checker"
  url = "http://instance:8929"
  id = 4
  token = "glrt-vst16ksxgXLPe7ozeS-r"
  token_obtained_at = 2023-07-04T09:54:01Z
  token_expires_at = 0001-01-01T00:00:00Z
  executor = "docker"
  [runners.cache]
    MaxUploadedArchiveSize = 0
  [runners.docker]
    tls_verify = false
    image = "python:latest"
    privileged = false
    disable_entrypoint_overwrite = false
    oom_kill_disable = false
    disable_cache = false
    volumes = ["/cache"]
    shm_size = 0

Hello @Divelix :wave:,

First off, please note that “For security and performance reasons, you should install GitLab Runner on a machine that is separate to the machine that hosts your GitLab instance.”. If you’re using this GitLab instance for anything other than local testing, I suggest you avoid hosting GitLab Runner and GitLab on the same machine.

Assmuing this setup is just for local testing, I believe this is an issue with Docker networking. The localhost inside your runner container is not the same localhost as your host machine or other containers. Each container has its own network namespace.

In your case, when you’re trying to access http://localhost:8929/root/runner_test.git/ from the runner container, it’s trying to access something from within the same container, not the instance container.

You’re on the right track by using the Docker bridge network and accessing the instance container using its name, like http://instance:8929/root/runner_test.git/. This is the correct way to access services in other containers.

Now, to make your runner container see the GitLab instance as localhost, you can use the extra_hosts option in your Docker Compose file. This option adds a line to /etc/hosts in the container, which allows you to redirect requests for a specific domain to a different IP.

Here’s an example of how you could modify your Docker Compose file:

runner:
  image: 'gitlab/gitlab-runner:latest'
  restart: always
  networks:
    - gitlab-network
  volumes:
    - '/srv/gitlab-runner/config:/etc/gitlab-runner'
    - '/var/run/docker.sock:/var/run/docker.sock'
  command:
    - 'run'
    - '--user=gitlab-runner'
    - '--working-directory=/home/gitlab-runner'
  extra_hosts:
    - "localhost:instance"

In this case, instance should be the IP address of your instance container. You can get this IP by running docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_name_or_id.

However, I would recommend sticking to using the service name (instance) instead of localhost to avoid confusion. You can change the url in your runner’s config.toml to http://instance:8929.

For more information, you can check out Docker’s networking documentation here.

2 Likes

Thank you for reply, @gitlab-greg! I tried to set:

extra_hosts:
  - "localhost:172.18.0.2"
depends_on:
  - instance # to make sure instance starts earlier and always gets IP 172.18.0.2

in docker-compose.yaml, but problem persists: I still get job error.
Also I tried to set

url = "http://instance:8929"

in runner’s config.toml even before asking for help here (without that I wasn’t even able to reach git cloning step).

Also I tried to set

[runners.docker]
  extra_hosts = ["localhost:172.18.0.2"]

in runner’s config.toml, but this didn’t help too.