Gitlab runner docker Could not resolve host

In case this helps others looking for this..

Same problem but GitLab and GitLab Runner run on different machines in LAN. DNS is working and ping gitlab works, except inside dockers:

Reproduce problem:

$ sudo docker run -it alpine ping gitlab
ping: bad address 'gitlab'
^C

But works with DNS given:

$ sudo docker run -it --dns=172.168.0.1 alpine ping gitlab
PING gitlab (172.168.0.5): 56 data bytes
64 bytes from 172.168.0.5: seq=0 ttl=63 time=0.536 ms
^C

Config actual LAN DNS for docker.

Edit /etc/docker/daemon.json on the GitLab Runner (file did not exist yet) with contents:

{
    "dns": ["172.168.0.1", "1.1.1.1"]
}

Test again, now OK:

$ sudo docker run -it --dns=172.168.0.1 alpine ping gitlab
PING gitlab (172.168.0.5): 56 data bytes
64 bytes from 172.168.0.5: seq=0 ttl=63 time=0.455 ms
64 bytes from 172.168.0.5: seq=1 ttl=63 time=0.905 ms
^C

If this is not how its supposed to be done, i'd be happy to hear.
If this problem shouldnt aught to exist in the first place, i'd be happy to hear as well. I was surprised to not find much references online to this problem for GitLab Runner..


Thanks to Tarun Lalwan link and according to Joyce Babu post, there are an undocumented option from the gitlab runner repos in the [runners.docker] section

network_mode : Add container to a custom network

So I have to set this option with my network name in the config.toml like

[[runners]]
  ...
  [runners.docker]
    ...
    network_mode = "gitlab_default"

OR when create the runner from command line

docker exec -it gitlab_gitlab-runner_1 gitlab-runner register \
--non-interactive \
--url http://gitlab_gitlab_1 \
--registration-token _wgMgEx3nBocYQtoi83c \
--executor docker \
--docker-image alpine:latest \
--docker-network-mode gitlab_default

I know this thread is old, but I saw everywhere threads pointing to this one, but did'nt solve for me.

I got the same error message :

fatal: unable to access 'http://gitlab.maison.fr:82/angular/test1.git/': Could not resolve host: gitlab.maison.fr

Adding network_mode = "host" to config.toml solve the problem.

My config.toml below on my local private network :

concurrent = 1
check_interval = 0

[session_server]
  session_timeout = 1800

[[runners]]
  name = "mars"
  url = "http://gitlab.maison.fr:82/"
  token = "TCfHAheTUdWMU2-fFNxK"
  executor = "docker"
  [runners.custom_build_dir]
  [runners.cache]
    [runners.cache.s3]
    [runners.cache.gcs]
    [runners.cache.azure]
  [runners.docker]
    tls_verify = false
    image = "gitlab/gitlab-runner:latest"
    privileged = false
    disable_entrypoint_overwrite = false
    oom_kill_disable = false
    disable_cache = false
    volumes = ["/cache"]
    shm_size = 0
    network_mode = "host"