![](/img/trans.png)
[英]how to run docker commands in remote machine via jenkins and SSH?
[英]Jenkins run sh in remote docker agent stuck
我配置了一个远程 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
我在这个问题上浪费了好几天,有什么想法吗?
经过几天的多次尝试,我找到了导致问题的关键点。
jenkinsci/blueocean
jenkins/inbound-agent:alpine
Inside both the Jenkins master/agent containers, I mounted the /var/run/docker.sock
into them, so that both the jenkins nodes could access the docker in the container, and agent docker
should be supported in our pipeline.
所以现在我制作管道脚本如下:
pipeline {
agent {
docker {
image 'python:latest'
label 'agent-hkgw'
}
}
stages {
stage('main') {
steps {
sh '''python --version'''
}
}
}
}
好吧,当我们构建它时,卡住了:
这就是我在问题中提到的问题。
在多次尝试失败后,我注意到从Z2E54334C0A5CE2E3E3E5A5845DF3AB3ADAZ 代理节点到管道构建容器的安装卷没有在Jenkins 代理节点中明确声明。 所以我尝试将/var/jenkins
文件夹从服务器 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)的 output 以进行调试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.