Gitlab runner, private repo, docker executor not using host's hosts file
Asked Answered
K

5

6

I have a private git repo. My runner is on a separate machine, both ubuntu. When I try ping $CI_REGISTRY in the yml file, I see during the build that the $CI_REGISTRY domain name is not resolving to the correct IP address. I need to hit the internal address of the server, not the external address so I set up a hosts file on the host on which gitlab runner is running that has the correct address, but the executor is ignoring it. Oddly, the address it's coming up with is an internal address on the cloudflare network, not the external address for the host I'm trying to reach as I would expect if it was doing a DNS lookup.

How can I either:

  • force the docker executor to use the host's hosts file
  • pass in an environment variable (or something) that the executor can use to resolve the address correctly
Keneth answered 16/7, 2020 at 23:10 Comment(0)
K
9

This issue was resolved by modifying /etc/gitlab-runner/config.toml:

[[runners]]
...
  [runners.docker]
  ...
    privileged = true
    extra_hosts = ["repo.mydomain.com:172.23.8.182"]
Keneth answered 18/7, 2020 at 15:56 Comment(0)
C
6

docker executor:

[[runners]]
  ...
  executor = "docker"
  [runners.docker]
    extra_hosts = ["gitlab.someweb.com:10.0.1.1"]

kubernetes executor:

[[runners]]
  ...
  executor = "kubernetes"
  [runners.kubernetes]
    [[runners.kubernetes.host_aliases]]
      ip = "10.0.1.1"
      hostnames = ["gitlab.someweb.com"]
Cavill answered 11/11, 2022 at 7:43 Comment(0)
S
1

You need to modify the container's /etc/hosts file, not the host's host file. The simplest way of doing this is the --add-host option.

Here's the documentation:

Add entries to container hosts file (--add-host)

You can add other hosts into a container’s /etc/hosts file by using one or more --add-host flags. This example adds a static address for a host named docker:

$ docker run --add-host=docker:10.180.0.1 --rm -it debian

root@f38c87f2a42d:/# ping docker
PING docker (10.180.0.1): 48 data bytes
56 bytes from 10.180.0.1: icmp_seq=0 ttl=254 time=7.600 ms
56 bytes from 10.180.0.1: icmp_seq=1 ttl=254 time=30.705 ms
^C--- docker ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max/stddev = 7.600/19.152/30.705/11.553 ms

(Source.)

Sommelier answered 17/7, 2020 at 2:25 Comment(4)
How does that work in the context of a gitlab runner executor? I am not launching the container directly - gitlab runner is.Keneth
Are you using the gitlab-runner command? If so, you can transform that into an equivalent docker run command using these instructions: docs.gitlab.com/runner/install/…Sommelier
No, the gitlab runner runs as a service, waits to pick up jobs when the CI pipeline makes them available then picks them up and executes them. After installation, I use the following instructions and choose docker as the executor: docs.gitlab.com/runner/registerKeneth
Can you try the --docker-extra-hosts option to gitlab-runner register?Sommelier
M
1

I tried several solutions but nothing worked until i simply entered the ip+port instead of the my fake domain name

Enter the GitLab instance URL (for example, https://gitlab.com/):
[http://gitlab_ip:port]
.....
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!

Sometimes it worth to think a bit before dive into stackoverflow :D

Muller answered 22/9, 2022 at 9:31 Comment(0)
R
1

You can use:

--docker-extra-hosts domainexample.com:x.x.x.x
Retrospective answered 20/2, 2023 at 10:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.