How do I get a Docker Gitlab CI runner to access Git on its parent host?

家住魔仙堡 提交于 2019-12-12 10:31:30

问题


Gitlab is setup on our internal network at http://gitlab

Docker containers on the same machine can't connect to it.

How do I configure docker so that it knows gitlab is its parent?


The problem exhibits itself when Gitlab CI attempts to run a build (inside a Docker container):

Cloning into '/builds/ns/project'... fatal: unable to access 'http://gitlab-ci-token:xxxxxx@gitlab/ns/project.git/': Couldn't resolve host 'gitlab'

I've tried adding the network's DNS servers to DOCKER_OPTS in /etc/default/docker to no avail.


回答1:


According to the GitLab CI Runner Advanced configuration, you can try to play with the extra_hosts param in your GitLab CI runner. In /etc/gitlab-runner/config.toml :

[[runners]]
  url = "http://gitlab/ci"
  token = "TOKEN"
  name = "my_runner"
  executor = "docker"
  [runners.docker]
    host = "tcp://<DOCKER_DAEMON_IP>:2375"
    image = "..."
    ...
    extra_hosts = ["gitlab:192.168.0.39"]

With this example, when inside the container running the test git will try to clone from gitlab, it will use the 192.168.0.39 as IP for this hostname.




回答2:


You can configure the container dns with the --dns flag to docker run (https://docs.docker.com/engine/reference/run/#network-settings), but it should use the host dns by default.




回答3:


In case your Gitlab installation also runs as a Docker container, just linking your gitlab-runner to the gitlab container should work:

docker run -d --name gitlab-runner --restart always --link gitlab \
-v /var/run/docker.sock:/var/run/docker.sock \
-v /srv/gitlab-runner/config:/etc/gitlab-runner gitlab/gitlab-runner:latest

This way Docker takes care of making the host 'gitlab' available inside gitlab-runner.



来源:https://stackoverflow.com/questions/34185388/how-do-i-get-a-docker-gitlab-ci-runner-to-access-git-on-its-parent-host

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!