欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 明星 > windows下使用docker执行器并配置 hosts 解析

windows下使用docker执行器并配置 hosts 解析

2025/2/25 17:36:00 来源:https://blog.csdn.net/weixin_44729138/article/details/145257170  浏览:    关键词:windows下使用docker执行器并配置 hosts 解析

本篇目录

    • 1. 问题背景
    • 2. 环境准备
      • 2.1 云上开通windows 2022 英文版机器
        • 2.1.1 安装 git
        • 2.1.2 安装 runner
        • 2.1.3 装docker
        • 2.1.4 注册runner并使用docker执行器
    • 3. 项目信息
      • 3.1 编写window bat脚本
      • 3.2 项目.gitlab-ci.yml文件
    • 4. 测试结论
      • 4.1 运行流水线
    • 5. troubleshooting
      • 问题1:Job failed: invalid volume specification: "/cache"
      • 问题2:Windows does not support privileged mode (docker.go:652:0s)
      • 问题3:extra_hosts配置生效问题

该实践来自于客户的一个真实需求

在这里插入图片描述

1. 问题背景

客户在 gitlab-runner 是使用的 windows 服务器上的 docker,客户表示在配置了extra_hosts后,发现并没有生效,无法实现某些需求。

2. 环境准备

2.1 云上开通windows 2022 英文版机器

2.1.1 安装 git

参考:安装git

2.1.2 安装 runner

参考:安装runner

2.1.3 装docker

参考: windows-docker

2.1.4 注册runner并使用docker执行器

runner的配置文件

concurrent = 1
check_interval = 0
connection_max_age = "15m0s"
shutdown_timeout = 0[session_server]session_timeout = 1800[[runners]]name = "windows"url = "https://jihulab.com"id = 31472token = "glrt-xxxxxxxxxx"token_obtained_at = 2024-08-06T10:34:29Ztoken_expires_at = 0001-01-01T00:00:00Zexecutor = "docker"[runners.custom_build_dir][runners.cache]MaxUploadedArchiveSize = 0[runners.cache.s3][runners.cache.gcs][runners.cache.azure][runners.docker]# extra_hosts = ["www.baidu.com:10.100.10.10"] # 经过反复多次的测试,发现该配置在windows下的docker执行器中不生效tls_verify = falseimage = "ruby:2.7"privileged = falsedisable_entrypoint_overwrite = falseoom_kill_disable = falsedisable_cache = falsevolumes = ["C:\\cache"] # 需要将["/cache"] 改成["C:\\cache"]shm_size = 0network_mtu = 0

3. 项目信息

3.1 编写window bat脚本

cat test.bat

@echo off
setlocal
rem 检查管理员权限
openfiles >nul 2>&1
if %errorlevel% neq 0 (echo 提升到管理员权限...powershell start-process -filepath '%0' -verb runasexit /b
)
rem 定义要添加的条目
set "hostEntry=192.168.10.100 www.baidu.com"
rem 检查条目是否已经存在
findstr /c:"%hostEntry%" %windir%\System32\drivers\etc\hosts >nul 2>&1
if %errorLevel% neq 0 (rem 添加条目到hosts文件echo %hostEntry% >> %windir%\System32\drivers\etc\hostsecho 条目已添加到hosts文件。
) else (echo 条目已存在于hosts文件中。
)
endlocal

3.2 项目.gitlab-ci.yml文件

cat .gitlab-ci.yml

default:tags:- windowsbuild:stage: buildimage: mcr.microsoft.com/dotnet/framework/sdk:4.8.1-windowsservercore-ltsc2022script:- ./test.bat- ping www.baidu.com

4. 测试结论

4.1 运行流水线

说明写入hosts后,解析生效
在这里插入图片描述
修改脚本(解析改成192.168.10.100和10.100.10.10),再次运行,如下两次结论:
192.168.10.100 无法解析
在这里插入图片描述
10.100.10.10 可以解析
在这里插入图片描述

5. troubleshooting

问题1:Job failed: invalid volume specification: “/cache”

在这里插入图片描述
原因:这个是由于默认runner注册的时候,volume的路径采用的是linux的路径方式,因为需要修改为windows的路径方式,将["/cache"]修改为["c:\\cache"]

问题2:Windows does not support privileged mode (docker.go:652:0s)

在这里插入图片描述
原因:这个是由于windows下runner的docker执行器不支持特权模式,因此将配置文件修改为privileged = trueprivileged = false

问题3:extra_hosts配置生效问题

extra_hosts本身是 docker 执行器的有效参数,但是通过在windows 上的 docker 和 linux 上的 docker 执行器上对比,linux 上是可以是生效的,但是 windows 上是不可以的,所以这里需要特殊说明下。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词