Jenkins构建Docker镜像时执行PowerShell脚本失败求助
问题
通过Jenkins使用Dockerfile在Docker环境中执行PowerShell脚本,此前相同的Dockerfile可正常运行,当前构建失败。
Dockerfile内容
FROM mcr.microsoft.com/powershell WORKDIR /app COPY htmlReport.ps1 /app COPY report.json /app RUN ["pwsh", "htmlReport.ps1"]
Jenkins报错信息
Step 1/5 : FROM mcr.microsoft.com/powershell latest: Pulling from powershell e96e057aae67: Pulling fs layer 06c4b2188acf: Pulling fs layer e96e057aae67: Verifying Checksum e96e057aae67: Download complete 06c4b2188acf: Verifying Checksum 06c4b2188acf: Download complete e96e057aae67: Pull complete 06c4b2188acf: Pull complete Digest: sha256:fe812277f0695af7846ad0cbf9a010e157ffa89ac78c9435b72ff0f1adb8435c Status: Downloaded newer image for mcr.microsoft.com/powershell:latest ---> 925e63c2bfe6 Step 2/5 : WORKDIR /app ---> Running in 8618b46d44dd Removing intermediate container 8618b46d44dd ---> 449979b5c145 Step 3/5 : COPY htmlReport.ps1 /app ---> 2f3b5600eedc Step 4/5 : COPY report.json /app ---> bf784e946fc2 Step 5/5 : RUN ["pwsh", "htmlReport.ps1"] ---> Running in 22e7ae2434ba Failed to create CoreCLR, HRESULT: 0x80070008 The command 'pwsh htmlReport.ps1' returned a non-zero code: 137 Build step 'Execute shell' marked build as failure
解决方法
排查内存资源不足
错误码137和HRESULT 0x80070008均指向内存不足问题:- 检查Jenkins主机的内存占用,关闭闲置进程或扩容主机内存。
- 构建时给Docker容器分配更多内存,比如在Jenkins的构建命令中添加参数:
docker build --memory 2g .。
固定PowerShell镜像版本
你使用的latest标签镜像可能被更新,导致兼容性问题:- 替换为之前能正常运行的特定版本,示例:
FROM mcr.microsoft.com/powershell:7.4.0-debian-bookworm - 可通过
docker images查看本地历史镜像版本,或从微软镜像仓库选择稳定版。
- 替换为之前能正常运行的特定版本,示例:
优化脚本内存占用
检查htmlReport.ps1和report.json:- 若
report.json文件过大,改用流式处理而非一次性加载到内存。 - 清理脚本中不再使用的变量、对象,避免内存泄漏。
- 若
调整Docker守护进程配置
修改Docker配置文件/etc/docker/daemon.json,增加内存配额:{ "resources": { "memory": "4g" } }重启Docker服务生效:
systemctl restart docker
内容的提问来源于stack exchange,提问作者BugBee
相关产品推荐
相关产品推荐

