简体   繁体   English

Jenkins 在远程 docker 代理中运行 sh 卡住

[英]Jenkins run sh in remote docker agent stuck

Problem问题

I configured a remote docker instance (on Server A) which enables tcp://server_a:2376 to serve the API.我配置了一个远程 docker 实例(在服务器 A 上),它启用tcp://server_a:2376来为 API 服务。

And I have a Jenkins server deployed on Server B, using (Docker jenkinsci/blueocean image).我在服务器 B 上部署了一个 Jenkins 服务器,使用(Docker jenkinsci/blueocean映像)。

Now I can access the Docker instance on Server A through TCP port:现在我可以通过 TCP 端口访问服务器 A 上的 Docker 实例:

DOCKER_HOST=tcp://<server_a>:2376 docker ps
DOCKER_HOST=tcp://<server_a>:2376 docker exec some_container "ls"

The above operations are fine.上面的操作没问题。

But when I make a Pipeline Script which runs through the Server-A-Docker as an agent, the problem comes out that the sh command stucks, with telling:但是当我制作一个通过 Server-A-Docker 作为代理运行的管道脚本时,问题就出现了sh命令卡住了,并告诉:

process apparently never started in /var/jenkins_home/workspace/agent-demo@tmp/durable-1ddcfc03进程显然从未在 /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) (使用 -Dorg.jenkinsci.plugins.durabletask.BourneShellScript.LAUNCH_DIAGNOSTICS=true 暂时运行 Jenkins 可能会使问题更清楚)


Pipeline Script流水线脚本

node {
    docker.withServer('tcp://<server_a>:2376') {
        docker.image('python:latest').inside() {
            sh "python --version"
        }
    }
}

Pipeline Console Output管道控制台 Output

在此处输入图像描述

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

I've been wasting days on the problem, any ideas?我在这个问题上浪费了好几天,有什么想法吗?

!!! !!! Finally !!!最后 !!!

After several days of several attempts, I found the key point which causes the problem.经过几天的多次尝试,我找到了导致问题的关键点。


Reproduction of the problem environment重现问题环境

  • Server A : Jenkins master node deployed under docker-compose using the image jenkinsci/blueocean服务器A :Jenkins主节点部署在docker-compose下,使用镜像jenkinsci/blueocean
  • Server B : Jenkins agent node(JNLP) deployed under docker-compose using image jenkins/inbound-agent:alpine服务器 B :Jenkins 代理节点(JNLP)部署在 docker-compose 下,使用图像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. 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.

So now I make the pipeline script as below:所以现在我制作管道脚本如下:

pipeline {
    agent {
        docker {
            image 'python:latest'
            label 'agent-hkgw'
        }
    }
    stages {
        stage('main') {
            steps {
                sh '''python --version'''
            }
            
        }
    }
}

Well, when we built it, Stucked:好吧,当我们构建它时,卡住了:

在此处输入图像描述

That's the issue that I mentioned in my question.这就是我在问题中提到的问题。


SOLUTION解决方案

After many ways of failed attempts, I notice that the mounted volume from Jenkins agent node to the Pipeline built container did not been explicitly declared in the Jenkins agent node .在多次尝试失败后,我注意到从Z2E54334C0A5CE2E3E3E5A5845DF3AB3ADAZ 代理节点管道构建容器的安装卷没有在Jenkins 代理节点中明确声明。 So I tried to mount the /var/jenkins folder from Server B to the Jenkins Agent Container :所以我尝试将/var/jenkins文件夹从服务器 B挂载到Jenkins 代理容器

在此处输入图像描述

Then the pipeline built works fine like a miracle!然后构建的管道像奇迹一样正常工作!

在此处输入图像描述

Hope this helps those who met the same problem in the future, thanks for your answers who attempted to offer help.希望这对将来遇到同样问题的人有所帮助,感谢您试图提供帮助的回答。

Okay, so this might sound silly, but try changing python --version to to python3 --version .好的,这听起来可能很愚蠢,但尝试将python --version 更改python3 --version

Also, make sure that your Python container has all appropriate paths in sh and bash .此外,请确保您的 Python 容器在shbash中具有所有适当的路径。

At last, make sure that the syntax for docker.image.inside() black for Python command execution is correct.最后,确保 Python 命令执行的 docker.image.inside() black 的语法正确。

Seems like you are missing the docker client in the Jenkins container.好像您在 Jenkins 容器中缺少 docker 客户端。 You need docker client to run docker commands locally or remotely您需要 docker 客户端在本地或远程运行 docker 命令

Try downloading/installing the docker client binary in your Jenkins container and ensure that it is in the system $PATH.尝试在 Jenkins 容器中下载/安装 docker 客户端二进制文件,并确保它位于系统 $PATH 中。

One way to install (may not work in your env - so ymmv)一种安装方式(可能无法在您的环境中工作 - 所以 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

Once the docker client is in the path, re-run the pipeline.一旦 docker 客户端在路径中,重新运行管道。 Also, print the output of echo $(which docker) in the pipeline for debugging purposes.此外,在管道中打印echo $(which docker)的 output 以进行调试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM