8

#gitlab-runner Could not resolve host

 2 years ago
source link: https://xmanyou.com/gitlab-runner-could-not-resolve-host/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
11 March 2021 / gitlab

#gitlab-runner Could not resolve host

用docker运行gitlab和gitlab-runner,并且二者运行在同一台机器上。

gitlab-runner正常注册,并使用docker executor。

在gitlab中添加作业以后,发现失败了,作业的执行日志如下:

Running with gitlab-runner 13.7.0 (943fc252)
  on default A8J5JmsQ
Preparing the "docker" executor
00:18
Using Docker executor with image python:3.6 ...
Pulling docker image python:3.6 ...
Using docker image sha256:6b0219e0ed757f23e9140c2efd0529e4a77aca47b1555e4ded26101ffe136d56 for python:3.6 with digest python@sha256:da022140db3b40d07c81815158092ff8ccfd967926b533a7c0b573eeeb5be120 ...
Preparing environment
00:00
Running on runner-a8j5jmsq-project-2-concurrent-0 via 52a38353b8ab...
Getting source from Git repository
00:01
Fetching changes with git depth set to 50...
Reinitialized existing Git repository in /builds/zhangzhibin/hello/.git/
fatal: unable to access 'http://9bc3f7e4d778/zhangzhibin/hello.git/': Could not resolve host: 9bc3f7e4d778
Cleaning up file based variables
00:01
ERROR: Job failed: exit code 1

gitlab-runner-job-error-could-not-resolve-host-01

从日志中可以发现,错误的地方是在runner试图从gitlab拉取代码时发生的。

其中9bc3f7e4d778是gitlab对应的容器名,也就是gitlab的容器机器名字。

没有配置的情况下,gitlab默认使用容器名做为外部访问地址:http://<容器名>

runner在执行作业的时候,会另外启动一个docker容器,而这个容器默认与gitlab不在同一个网络中,无法识别gitlab的host。

  • 1). 将二者添加到同一个network下。
    先查看gitlab所在的network
docker network ls

然后,在gitlab-runner的配置文件config.toml添加network_mode参数,添加在[runners.docker]下

  [runners.docker]
  ...
    network_mode = "<gitlab 所在的network>"
  ...

重启gitlab-runner

  • 2). 更改git clone的地址
    在gitlab-runner的配置文件config.toml添加clone_url参数,添加在[[runners]]的全局配置中
[[runners]]
...
clone_url = "gitlab的外部访问url"
...

如果遇到修改后,gitlab-runner无法启动,甚至报错:

gitlab-runner    | FATAL: Service run failed                           error=toml: cannot load TOML value of type []interface {} into a Go string

gitlab-runner-load-toml-value-failed

可以尝试将旧的config.toml备份,然后创建一个新的。

阿斌

Read more posts by this author.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK