Gitlab runner docker Could not resolve host

2020-03-12 11:10发布

Im using 2 containers on my Ubuntu OS: Gitlab-ce and gitlab-runner

Containers names are: gitlab_gitlab_1 and gitlab_gitlab-runner_1

I access to my gitlab app via gitlab.localhost.com:801

I register successfully a runner with this command:

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

Then, when I start the job, I got this error message:

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

In both container, I can access to the hostname gitlab.localhost.com. I think the issue come from the image alpine which can not resolve the host.

How can I fix that ?

Thanks

Edit 1

docker-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'

Edit 2

docker-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'

3条回答
Summer. ? 凉城
2楼-- · 2020-03-12 11:28

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
查看更多
相关推荐>>
3楼-- · 2020-03-12 11:40

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

查看更多
欢心
4楼-- · 2020-03-12 11:53

As I can see you have defined a network already which means that both gitlab and gitlab-runner are in the same network. you can verify that by using docker inspect. so you need to remove links as you don't need it.

In order to set a network alias you need to change the network part at gitlab service to the following:

gitlab:
  ...
  networks:
    default:
      aliases:
        - gitlab.localhost.com

References:

查看更多
登录 后发表回答