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

Viewed 2930

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
3 Answers

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"]

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.)

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

Related