Docker GitLab-Runner安装

文章讲述了如何安装GitLab-Runner,包括拉取镜像、运行容器,以及在遇到域名解析失败的问题后,通过修改hosts文件解决注册问题的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

GitLab-Runner

安装

  1. 拉去gitlab/gitlab-runner镜像
docker pull gitlab/gitlab-runner:latest
  1. 运行容器:
docker run -d --name runner-java \
  --restart always \
  -v /var/run/docker.sock:/var/run/docker.sock \
  -v /opt/docker/gitlab-runner/runner-java/config:/etc/gitlab-runner \
  gitlab/gitlab-runner:latest
  1. 进去容器
docker exec -it gitlab-runner /bin/bash
  1. 开始注册流程

获取url和token:
点击设置页面
在这里插入图片描述
概览 --> Runner --> 注册一个实例Runner
在这里插入图片描述

获取url和token
请添加图片描述

注册命令:

gitlab-runner register

root@c3faf0ff4921:/# gitlab-runner register
Runtime platform                                    arch=amd64 os=linux pid=50 revision=d89a789a version=16.4.1
Running in system-mode.                            
                                                   
Enter the GitLab instance URL (for example, https://gitlab.com/):
http://home.zsl0.com:9080/         
Enter the registration token:
xxxxx
Enter a description for the runner:
[c3faf0ff4921]: 
Enter tags for the runner (comma-separated):
runner-java
Enter optional maintenance note for the runner:

WARNING: Support for registration tokens and runner parameters in the 'register' command has been deprecated in GitLab Runner 15.6 and will be replaced with support for authentication tokens. For more information, see https://docs.gitlab.com/ee/ci/runners/new_creation_workflow 
Registering runner... succeeded                     runner=SdP2xtsF
Enter an executor: shell, ssh, virtualbox, docker-autoscaler, docker+machine, custom, docker, parallels, instance, docker-windows, kubernetes:
docker
Enter the default Docker image (for example, ruby:2.7):
docker 
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
 
Configuration (with the authentication token) was saved in "/etc/gitlab-runner/config.toml" 

(其他注册方式,未使用这种)使用docker命令注册:

docker exec -it gitlab-runner gitlab-runner register --non-interactive --executor "docker" --docker-image alpine:latest --url "https://xx.xx.com"  --registration-token "RQxx7UtMssiWyzWqtUMr" --description "172.30.7.17-docker-runner" --tag-list "docker-runner,share-runner,172.30.7.17-runner" --run-untagged="true"  --locked="false"  --access-level="not_protected"
  1. 编辑runner 勾选’运行未标记的作业’保存:

注:图片中名字runner-file-service为注册时填写tags标签,可自行设置

在这里插入图片描述
勾选’运行未标记的作业’ --> 保存
在这里插入图片描述

  1. 修改confi.toml配置文件
    敏感信息使用xxx替换,剩下的根据个人需求进行选择修改
concurrent = 10
check_interval = 0
shutdown_timeout = 0

[session_server]
  session_timeout = 1800

[[runners]]
  # 用于配置远程镜像仓库拉取的认证
  environment = ['DOCKER_AUTH_CONFIG={"auths": {"registry.cn-hangzhou.aliyuncs.com/zsl0/maven-build": {"auth": "xxxxxx"}}}']
  name = "10e4d34ff34f"
  url = "http://192.168.1.81:9080/"
  id = 2
  token = "xxxx"
  token_obtained_at = 2023-10-20T03:24:03Z
  token_expires_at = 0001-01-01T00:00:00Z
  executor = "docker"
  [runners.cache]
    MaxUploadedArchiveSize = 0
  [runners.docker]
    tls_verify = false
    image = "maven:latest"
    privileged = false
    disable_entrypoint_overwrite = false
    oom_kill_disable = false
    disable_cache = false
    # 用于docker in docker
    volumes = ["/var/run/docker.sock:/var/run/docker.sock","/cache"]
    shm_size = 0

问题合集

GitLab 域名的配置修改

GitLab 域名的配置修改

Runner容器内注册失败,提示 ‘dial tcp: lookup home.zsl0.com on 192.168.254.2:53: no such host’

报错信息:

root@c3faf0ff4921:/# gitlab-runner register
Runtime platform                                    arch=amd64 os=linux pid=25 revision=d89a789a version=16.4.1
Running in system-mode.                            
                                                   
Enter the GitLab instance URL (for example, https://gitlab.com/):
http://home.zsl0.com:9080/
Enter the registration token:
xxxxx
Enter a description for the runner:
[c3faf0ff4921]: 
Enter tags for the runner (comma-separated):
runner-file-service
Enter optional maintenance note for the runner:

WARNING: Support for registration tokens and runner parameters in the 'register' command has been deprecated in GitLab Runner 15.6 and will be replaced with support for authentication tokens. For more information, see https://docs.gitlab.com/ee/ci/runners/new_creation_workflow 
ERROR: Registering runner... failed                 runner=SdP2xtsF status=couldn't execute POST against http://home.zsl0.com:9080/api/v4/runners: Post "http://home.zsl0.com:9080/api/v4/runners": dial tcp: lookup home.zsl0.com on 192.168.254.2:53: no such host
PANIC: Failed to register the runner.    

解决办法(临时解决):
因为猜测是没办法解析域名导致,正好运行Runner的宿主机也是运行GitLab的机器,直接去找宿主机就行了,所以执行

echo ‘172.17.0.1 home.zsl0.com’ >> /etc/hosts

重新注册命令:

gitlab-runner register

root@c3faf0ff4921:/# gitlab-runner register
Runtime platform                                    arch=amd64 os=linux pid=50 revision=d89a789a version=16.4.1
Running in system-mode.                            
                                                   
Enter the GitLab instance URL (for example, https://gitlab.com/):
http://home.zsl0.com:9080/         
Enter the registration token:
xxxxx
Enter a description for the runner:
[c3faf0ff4921]: 
Enter tags for the runner (comma-separated):
runner-file-service
Enter optional maintenance note for the runner:

WARNING: Support for registration tokens and runner parameters in the 'register' command has been deprecated in GitLab Runner 15.6 and will be replaced with support for authentication tokens. For more information, see https://docs.gitlab.com/ee/ci/runners/new_creation_workflow 
Registering runner... succeeded                     runner=SdP2xtsF
Enter an executor: shell, ssh, virtualbox, docker-autoscaler, docker+machine, custom, docker, parallels, instance, docker-windows, kubernetes:
docker
Enter the default Docker image (for example, ruby:2.7):
docker 
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
 
Configuration (with the authentication token) was saved in "/etc/gitlab-runner/config.toml" 
### 使用 Docker 部署 GitLab Runner 的指南 为了使用 Docker 容器化技术来部署 GitLab Runner,可以遵循一系列特定的操作流程。这不仅简化了环境设置过程,还提高了不同开发阶段的一致性和可移植性。 #### 准备工作 确保本地机器上已经安装并配置好 Docker 环境。如果尚未完成此操作,则需先访问官方文档获取详细的安装指导[^2]。 #### 下载 GitLab Runner 镜像 通过执行如下命令下载最新的 GitLab Runner Docker 镜像: ```bash docker pull gitlab/gitlab-runner:latest ``` #### 创建专用网络 (推荐) 为提高容器间通信的安全性和效率,建议创建一个新的自定义桥接网络用于 GitLab 和其关联服务之间的交互: ```bash docker network create gitlab-network ``` #### 启动 GitLab Runner 容器实例 启动 GitLab Runner 实例时指定必要的参数以实现最佳性能和功能支持。下面是一个典型的 `docker run` 命令示例,其中包含了注册 token (`REGISTRATION_TOKEN`) 及其他重要选项: ```bash docker run -d --name gitlab-runner \ --network gitlab-network \ -v /srv/gitlab-runner/config:/etc/gitlab-runner \ -v /var/run/docker.sock:/var/run/docker.sock \ gitlab/gitlab-runner:latest ``` 这里 `-v` 参数用来挂载主机上的路径至容器内部;特别是 `/var/run/docker.sock` 文件映射允许 GitLab Runner 访问宿主机的 Docker Daemon 进而构建项目镜像。 #### 注册 GitLab Runner 到 CI/CD 流水线 一旦容器成功运行起来之后,就需要将其注册到目标 GitLab 实例中去参与持续集成管道的工作。可以通过向上述创建好的容器发送 HTTP 请求或者直接进入容器内部执行注册脚本来达成目的。具体做法取决于所使用的 GitLab 版本和个人偏好。 对于基于 Web UI 方式的注册方法,请参照官方手册中的说明进行操作[^3]。 #### 自动化配置 Maven 私服连接 当涉及到 Java 或者 Kotlin 类型的应用程序时,在 `.gitlab-ci.yml` 中添加适当的前置指令能够帮助自动复制预设好的 `settings.xml` 至默认位置从而顺利完成依赖解析任务。 ```yaml before_script: - mkdir -p ~/.m2 - cp .mvn/conf/settings.xml ~/.m2/ ``` 以上步骤展示了如何利用 Docker 技术快速搭建起一个可用的 GitLab Runner 并使其参与到自动化测试与发布过程中来。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值