Gitlab runner docker无法解析主机

时间:2018-05-14 08:24:16

标签: docker gitlab-ci gitlab-ci-runner

我在我的Ubuntu操作系统上使用了2个容器:Gitlab-ce和gitlab-runner

容器名称为:gitlab_gitlab_1gitlab_gitlab-runner_1

我通过gitlab.localhost.com:801

访问我的gitlab应用

我使用此命令成功注册了一名跑步者:

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

然后,当我开始工作时,我收到了以下错误消息:

Running with gitlab-runner 10.7.1 (b9bba623)
  on 589a617ee407 12ba77f7
Using Docker executor with image alpine:latest ...
Pulling docker image alpine:latest ...
Using docker image sha256:3fd9065eaf02feaf94d68376da52541925650b81698c53c6824d92ff63f98353 for alpine:latest ...
Running on runner-12ba77f7-project-1-concurrent-0 via 01196621a827...
Cloning repository...
Cloning into '/builds/root/test'...
fatal: unable to access 'http://gitlab-ci-token:xxxxxxxxxxxxxxxxxxxx@gitlab.localhost.com/root/test.git/': Could not resolve host: gitlab.localhost.com
ERROR: Job failed: exit code 1

在这两个容器中,我都可以访问主机名gitlab.localhost.com。 我认为这个问题来自高山图像无法解决主机问题。

我该如何解决?

由于

修改1

搬运工-compose.yml

version: '3'
services:
  gitlab:
    image: 'gitlab/gitlab-ce:latest'
    restart: always
    hostname: 'gitlab.localhost.com'
    environment:
      GITLAB_OMNIBUS_CONFIG: |
        external_url 'http://gitlab.localhost.com'
    ports:
      - '801:80'
      - '443:443'
      - '22:22'
    volumes:
      - '/srv/gitlab/config:/etc/gitlab'
      - '/srv/gitlab/logs:/var/log/gitlab'
      - '/srv/gitlab/data:/var/opt/gitlab'
    networks:
      - 'default'

  gitlab-runner:
    image: 'gitlab/gitlab-runner:latest'
    depends_on:
      - 'gitlab'
    restart: always
    volumes:
      - '/srv/gitlab-runner/config:/etc/gitlab-runner'
      - '/var/run/docker.sock:/var/run/docker.sock'
    networks:
      - 'default'
    links:
      - 'gitlab:gitlab.localhost.com'

networks:
  default:
    driver: 'bridge'

修改2

搬运工-compose.yml

version: '3'
services:
  gitlab:
    image: 'gitlab/gitlab-ce:latest'
    restart: always
    hostname: 'gitlab.localhost.com'
    environment:
      GITLAB_OMNIBUS_CONFIG: |
        external_url 'http://gitlab.localhost.com'
    ports:
      - '801:80'
      - '443:443'
      - '22:22'
    volumes:
      - '/srv/gitlab/config:/etc/gitlab'
      - '/srv/gitlab/logs:/var/log/gitlab'
      - '/srv/gitlab/data:/var/opt/gitlab'
    networks:
      default:
        aliases:
          - 'gitlab.localhost.com'

  gitlab-runner:
    image: 'gitlab/gitlab-runner:latest'
    depends_on:
      - 'gitlab'
    restart: always
    volumes:
      - '/srv/gitlab-runner/config:/etc/gitlab-runner'
      - '/var/run/docker.sock:/var/run/docker.sock'
    networks:
      - 'default'

networks:
  default:
    driver: 'bridge'

4 个答案:

答案 0 :(得分:12)

感谢Tarun Lalwan链接,根据Joyce Babu的帖子,gitlab的跑步者repos中有一个undocumented option  [runners.docker] section

network_mode : Add container to a custom network

所以我必须在config.toml之类的

中使用我的网络名称设置此选项
[[runners]]
  ...
  [runners.docker]
    ...
    network_mode = "gitlab_default"

从命令行创建运行器时的OR

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

答案 1 :(得分:1)

我知道这个线程很旧,但是我到处都看到指向这个线程的线程, 但没有为我解决。

我收到了相同的错误消息:

致命:无法访问'http://gitlab.maison.fr:82/angular/test1.git/':无法解析主机:gitlab.maison.fr

config.toml 中添加 network_mode =“ host” 即可解决问题。

下面我的本地专用网络上的config.toml:

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"

答案 2 :(得分:0)

我可以看到您已经定义了一个网络,这意味着gitlab-runnerdocker inspect都在同一个网络中。您可以使用links进行验证。因此,您需要删除gitlab,因为您不需要它。

要设置网络别名,您需要将gitlab: ... networks: default: aliases: - gitlab.localhost.com 服务的网络部分更改为以下内容:

public function listAction(RegionsService $service)
{
    $regions = $service->getRegionList();

    $editForm = $this->createForm('App\Form\RegionListType');

    if ($editForm->isSubmitted())
    {
        dump('submitted');
        die();
    }

    if ($editForm->isSubmitted() && $editForm->isValid()) {

        $task = $editForm->getData();
        dump($task);
        die();
        ...
    }        

    return $this->render('parameter/region.list.html.twig', [
        'form'   => $editForm->createView(),
        'regions'   => $regions

    ]);
    ...

参考文献:

答案 3 :(得分:0)

以防其他人寻找它。

相同的问题,但是GitLab和GitLab Runner在LAN中的不同计算机上运行。 DNS工作正常,ping gitlab工作正常,但docker内部除外:

重现问题:

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

但是可以使用给定的DNS:

$ 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

为docker配置实际的LAN DNS。

在GitLab Runner上编辑/etc/docker/daemon.json(文件尚不存在),内容如下:

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

再次测试,现在确定:

$ 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

如果这不是应该做的,我很高兴听到。
如果首先不应该存在这个问题,我也很高兴听到。令我惊讶的是,在网上找不到有关GitLab Runner的此问题的大量参考。