管道中的构建步骤因连接被拒绝错误而失败,而 运行 GitLab 和 GitLab-Runner docker 本地实例

Build step in pipeline is failing with connection refused error while running GitLab and GitLab-Runner docker instances locally

我在本地 运行ning GitLab 和 Gitlab-Runner docker 个实例。当执行 Spring Boot 和 Maven 项目管道时,出现以下错误。

Getting source from Git repository
00:02
 Fetching changes with git depth set to 50...
 Reinitialized existing Git repository in /builds/root/starter-springboot-pipeline/.git/
 fatal: unable to access 'http://localhost/root/starter-springboot-pipeline.git/': Failed to connect to localhost port 80: Connection refused
Uploading artifacts for failed job
00:07
 ERROR: Job failed: exit code 1

不确定上面错误中的localhost是指GitLab容器还是Runner容器。它应该指的是 gitlab 容器而不是本地主机吗?

下面是我使用的命令和配置。

启动 GitLab 服务器:

docker run -itd --network=gitlab-network --hostname localhost \
           --publish 443:443 --publish 80:80 --publish 22:22 \
           --name gitlab --restart always --volume config:/etc/gitlab \
           --volume logs:/var/log/gitlab \
           --volume data:/var/opt/gitlab \
           gitlab/gitlab-ee:12.10.14-ee.0

启动 GitLab Runner

docker run -d --name gitlab-runner --restart always \
     -v ~/gitlab/gitlab-runner/config:/etc/gitlab-runner \
     -v /var/run/docker.sock:/var/run/docker.sock \
     gitlab/gitlab-runner:v12.10.3

已创建网络 'gitlab-network' 并将两个容器添加到其中。

docker network connect gitlab-network gitlab
docker network connect gitlab-network gitlab-runner

注册 Runner 如下:

Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
http://gitlab
Please enter the gitlab-ci token for this runner:
XxXXxXXXxxXXXXXX
Please enter the gitlab-ci description for this runner:
[49ad685039ad]: runner14
Please enter the gitlab-ci tags for this runner (comma separated):
docker
Registering runner... succeeded                     runner=EkWnb63h
Please enter the executor: docker-ssh, parallels, shell, virtualbox, docker+machine, kubernetes, custom, docker, ssh, docker-ssh+machine:
docker
Please enter the default Docker image (e.g. ruby:2.6):
alpine:latest
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!

下面是gitlab-ci.yml

image: maven:3.3-jdk-8
stages:
  - test
  
test_job:
  stage: test
  script:
    - pwd
    - mvn clean
    - mvn compile
    - mvn test
  tags:
    - docker

我最近开始在 GitLab 和 docker 上工作,能够在通过大量研究解决一些问题后设置它们和 运行 管道。但是我被这个问题困住了。

主机名 localhost 总是解析为 127.0.0.1::1 - 换句话说,就是回环接口,顾名思义,回环并连接到 iteslf。

这意味着你的 运行ner 正试图在它自己的容器中找到 http://localhost/root/starter-springboot-pipeline.git/ - 这显然失败了,因为它在 GitLab 的容器中。

这也是为什么在您的 运行ner 配置中,您必须将 GitLab 的地址指定为 http://gitlab 而不是 http://localhost

您可以尝试使用命令启动 GitLab 容器(预先重新创建命名卷以确保它是从头开始配置的)

docker run -itd --network=gitlab-network --hostname gitlab \
           --publish 443:443 --publish 80:80 --publish 22:22 \
           --name gitlab --restart always \
           --env GITLAB_OMNIBUS_CONFIG="external_url 'http://gitlab/'" \
           --volume config:/etc/gitlab \
           --volume logs:/var/log/gitlab \
           --volume data:/var/opt/gitlab \
           gitlab/gitlab-ee:12.10.14-ee.0

但我不能保证它会工作,因为 GitLab 被设计为 运行 在具有自己唯一主机名的服务器上。

编辑:

您也可以尝试编辑 config.toml 并将 [runners.docker] 部分中的 network_mode 设置为 gitlab-network。有关详细信息,请参阅 here