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

本地Docker容器部署GitLab与Runner的网络配置故障排查

Docker Compose部署GitLab+GitLab-Runner的网络连接问题

正常运行状态

  • GitLab和GitLab-Runner容器均成功启动运行
  • GitLab可作为本地代码仓库远程端,支持克隆、推送等操作
  • GitLab-Runner能正常为gitlab.com等外部GitLab服务器的项目执行任务
  • Runner可成功注册到GitLab容器内的项目,能查看配置、触发任务(Docker执行器会启动并拉取运行环境镜像)

异常问题

任务拉取项目源码失败,错误信息:

fatal: unable to access 'http://gitlab:8081/cours/pipelinediscover.git/': Failed to connect to gitlab port 8081 after 131012 ms: Couldn't connect to server

相关配置文件

1. Docker Compose配置文件

version: '3.6'
services:
  gitlab:
    image: 'gitlab/gitlab-ce:latest'
    restart: always
    container_name: gitlab
    hostname: 'gitlab'
    environment:
      GITLAB_OMNIBUS_CONFIG: 
        external_url 'http://gitlab:8081'
        # Add any other gitlab.rb configuration here, each on its own line
    ports:
      - '8081:8081'
      - '4443:443'
      - '2222:22'
    volumes:
      - './config:/etc/gitlab'
      - './logs:/var/log/gitlab'
      - './data:/var/opt/gitlab'
    shm_size: '1024m'
  runner:
    image: 'gitlab/gitlab-runner:latest'
    restart: always
    volumes:
      - './gitlab-runner/config:/etc/gitlab-runner'
      - '/var/run/docker.sock:/var/run/docker.sock'
    container_name: gitlab-runner

注:必须修改主机/etc/hosts文件,才能通过gitlab:8081访问GitLab界面,而非localhost:8081。但如果将GitLab的external_url配置为localhost:8081,GitLab-Runner完全无法与GitLab交互。

2. GitLab-Runner配置文件(config.toml)

concurrent = 5
check_interval = 0
shutdown_timeout = 0

[session_server]
  session_timeout = 1800

[[runners]]
  name = "05f411d789cb"
  url = "http://gitlab:8081/"
  id = 6
  token = "9HXz2_XQzLCpqPiTqDUZ"
  token_obtained_at = 2023-03-31T07:09:00Z
  token_expires_at = 0001-01-01T00:00:00Z
  executor = "docker"
  [runners.cache]
    MaxUploadedArchiveSize = 0
  [runners.docker]
    tls_verify = false
    image = "alpine:latest"
    privileged = false
    disable_entrypoint_overwrite = false
    oom_kill_disable = false
    disable_cache = false
    volumes = ["/cache"]
    shm_size = 0

问题根源与解决方法

根源

使用Docker执行器时,Runner会启动新的子容器执行任务,这些子容器默认不在GitLab和Runner所在的Compose网络中,因此无法解析gitlab主机名,导致无法连接GitLab仓库。

解决步骤

  1. 为Compose服务配置自定义网络
    在docker-compose.yml中添加自定义网络,让GitLab、Runner以及Runner启动的子容器都加入该网络:

    version: '3.6'
    # 添加自定义网络
    networks:
      gitlab-network:
        driver: bridge
    services:
      gitlab:
        # ... 原有配置保持不变
        networks:
          - gitlab-network
      runner:
        # ... 原有配置保持不变
        networks:
          - gitlab-network
    
  2. 修改Runner配置,让子容器加入自定义网络
    在Runner的config.toml中,给[runners.docker]添加network_mode配置,指定为自定义网络名称:

    [[runners]]
      # ... 原有配置保持不变
      [runners.docker]
        # ... 原有配置保持不变
        network_mode = "gitlab-network" # 与Compose中定义的网络名称一致
    
  3. 重启服务
    停止并重新启动所有容器:

    docker-compose down
    docker-compose up -d
    
  4. 可选:优化GitLab访问配置
    若不想修改主机/etc/hosts,可将GitLab的external_url改为http://localhost:8081,Runner注册时仍使用GitLab容器的内部地址http://gitlab:8081,这样主机可通过localhost:8081访问界面,Runner也能正常与GitLab交互。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 12:47:40