You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

能否自定义GitLab runner执行作业时生成的Docker容器名称?

GitLab Runner作业容器自定义与高负载定位方案

自定义作业容器名称前缀

GitLab Runner Docker执行器原生支持配置作业容器名前缀,直接修改Runner配置即可实现:
找到你挂载到宿主机的GitLab Runner配置文件config.toml(通常在/etc/gitlab-runner对应挂载目录下),在目标Runner的[runners.docker]配置块中添加container_name_prefix字段,值为你需要的自定义前缀:

[[runners]]
  name = "prod-runner"
  url = "https://your-gitlab-domain.com"
  token = "runner-registration-token"
  executor = "docker"
  [runners.docker]
    # 自定义前缀示例,可按业务场景命名
    container_name_prefix = "fe-ci-build-"
    tls_verify = false
    image = "node:20-alpine"
    # 其余原有配置无需改动

修改完成后重启gitlab-runner容器,后续新启动的作业容器名称会自动带上该前缀,格式为fe-ci-build-runner-xkawnaf-project-xxx-concurrent-x-xxxx。
注意:不要配置全固定的容器名,Docker不支持同名容器同时运行,前缀+默认生成后缀的形式不会影响Runner并发执行作业。

快速定位高负载作业容器的其他方法

如果不想修改Runner配置,利用GitLab Runner自动注入的容器标签可以更高效定位问题,比识别容器名更灵活:

  • 所有Runner启动的作业容器默认携带完整CI元数据标签,核心标签包括:
    • com.gitlab.gitlab-runner.project.id:对应GitLab项目ID
    • com.gitlab.gitlab-runner.job.id:对应CI作业ID
    • com.gitlab.gitlab-runner.job.name:对应CI作业名称
    • com.gitlab.gitlab-runner.pipeline.id:对应流水线ID
  • 直接查看所有CI作业容器的CPU、内存占用:
docker stats $(docker ps -q --filter "label=com.gitlab.gitlab-runner.job.id")
  • 拿到高负载容器ID后,一键查询对应的CI作业信息,无需手动匹配容器名:
# 替换为实际的高负载容器ID
docker inspect 7c2b650d29a5 --format '作业名: {{ index .Config.Labels "com.gitlab.gitlab-runner.job.name" }} | 项目ID: {{ index .Config.Labels "com.gitlab.gitlab-runner.project.id" }} | 流水线ID: {{ index .Config.Labels "com.gitlab.gitlab-runner.pipeline.id" }}'
  • 你也可以在[runners.docker]配置块中通过container_labels字段给该Runner启动的所有容器添加自定义标签,比如标记所属团队、环境、用途,后续过滤排查效率更高:
[runners.docker]
  container_labels = {"env"="prod", "owner"="frontend-team", "type"="build"}

内容的提问来源于stack exchange,提问作者Dennys

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.21 16:16:02