如何解决Jenkins 在远程 docker 代理中运行 sh 卡住 问题管道脚本管道控制台输出!!!终于!!!问题环境重现解决方案
问题
我配置了一个远程 docker 实例(在服务器 A 上),它使 tcp://server_a:2376
能够为 API 提供服务。
我在服务器 B 上部署了一个 Jenkins 服务器,使用(Docker jenkinsci/blueocean
映像)。
现在我可以通过 TCP 端口访问服务器 A 上的 Docker 实例了:
DOCKER_HOST=tcp://<server_a>:2376 docker ps
DOCKER_HOST=tcp://<server_a>:2376 docker exec some_container "ls"
以上操作没问题。
但是当我制作一个通过 Server-A-Docker 作为代理运行的流水线脚本时,问题出现了 sh
命令卡住了,并告诉:
进程显然从未在 /var/jenkins_home/workspace/agent-demo@tmp/durable-1ddcfc03 中启动
(使用 -Dorg.jenkinsci.plugins.durabletask.BourneshellScript.LAUNCH_DIAGNOSTICS=true 临时运行 Jenkins 可能会使问题更清晰)
管道脚本
node {
docker.withServer('tcp://<server_a>:2376') {
docker.image('python:latest').inside() {
sh "python --version"
}
}
}
管道控制台输出
Started by user iotsofttest
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/agent-demo
[Pipeline] {
[Pipeline] withDockerServer
[Pipeline] {
[Pipeline] isUnix
[Pipeline] sh
+ docker inspect -f . python:latest
.
[Pipeline] withDockerContainer
Jenkins seems to be running inside container 5be8fc34c80a55ddcc2f5399009b97260adfc7ba9ef88985e0f7df614c707b42
but /var/jenkins_home/workspace/agent-demo Could not be found among []
but /var/jenkins_home/workspace/agent-demo@tmp Could not be found among []
$ docker run -t -d -u 0:0 -w /var/jenkins_home/workspace/agent-demo -v /var/jenkins_home/workspace/agent-demo:/var/jenkins_home/workspace/agent-demo:rw,z -v /var/jenkins_home/workspace/agent-demo@tmp:/var/jenkins_home/workspace/agent-demo@tmp:rw,z -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** python:latest cat
$ docker top 25dccce629d42d82b177c79544cdcd2675bad8daf94f11c55f7f9821eb6e052e -eo pid,comm
[Pipeline] {
[Pipeline] sh
process apparently never started in /var/jenkins_home/workspace/agent-demo@tmp/durable-1ddcfc03
(running Jenkins temporarily with -Dorg.jenkinsci.plugins.durabletask.BourneshellScript.LAUNCH_DIAGNOSTICS=true might make the problem clearer)
[Pipeline] }
$ docker stop --time=1 25dccce629d42d82b177c79544cdcd2675bad8daf94f11c55f7f9821eb6e052e
$ docker rm -f 25dccce629d42d82b177c79544cdcd2675bad8daf94f11c55f7f9821eb6e052e
[Pipeline] // withDockerContainer
[Pipeline] }
[Pipeline] // withDockerServer
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code -2
Finished: FAILURE
我在这个问题上浪费了好几天,有什么想法吗?
解决方法
!!!终于!!!
经过几天的多次尝试,我找到了导致问题的关键点。
问题环境重现
-
服务器 A:使用镜像
**/a/b/*.ekv
在 docker-compose 下部署的 Jenkins 主节点
-
服务器 B:使用镜像
jenkinsci/blueocean
在 docker-compose 下部署的 Jenkins 代理节点(JNLP)
在两个 Jenkins 主/代理容器中,我将 jenkins/inbound-agent:alpine
安装到其中,以便两个 jenkins 节点都可以访问容器中的 docker,并且我们的管道中应该支持 /var/run/docker.sock
。
所以现在我制作管道脚本如下:
agent docker
好吧,当我们构建它时,卡住了:
这就是我在问题中提到的问题。
解决方案
经过多次尝试失败后,我注意到从 Jenkins 代理节点 到 Pipeline 构建的容器 的挂载卷没有在 Jenkins 中明确声明代理节点。所以我尝试将 pipeline {
agent {
docker {
image 'python:latest'
label 'agent-hkgw'
}
}
stages {
stage('main') {
steps {
sh '''python --version'''
}
}
}
}
文件夹从 Server B 挂载到 Jenkins 代理容器:
然后构建的管道像奇迹一样正常运行!
希望这对以后遇到同样问题的人有所帮助,感谢您试图提供帮助的回答。
,好的,所以这听起来可能很傻,但请尝试将 python --version 更改为 到 python3 --version。
此外,请确保您的 Python 容器在 sh 和 bash 中具有所有适当的路径。
最后确保python命令执行的docker.image.inside() black的语法正确。
,您似乎缺少 Jenkins 容器中的 docker 客户端。您需要 docker 客户端在本地或远程运行 docker 命令
尝试在您的 Jenkins 容器中下载/安装 docker 客户端二进制文件,并确保它在系统 $PATH 中。
一种安装方式(可能不适用于您的环境 - 所以 ymmv)
curl -fsSLO https://get.docker.com/builds/Linux/x86_64/docker-17.03.1-ce.tgz &&
tar --strip-components=1 -xvzf docker-17.03.1-ce.tgz -C /usr/local/bin
一旦 docker 客户端在路径中,重新运行管道。此外,为了调试目的,在管道中打印 echo $(which docker) 的输出。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。