解决pipline找不到docker命令:/Users/beck/.jenkins/workspace/locust_test@tmp/durable-884e1b18/script.sh: line 1: docker: command not found

问题一:/Users/beck/.jenkins/workspace/locust_test@tmp/durable-884e1b18/script.sh: line 1: docker: command not found

完整的pipeline如下,在docker hub上我打包了locust的运行环境locust:v1,这时候通过本地的jenkins去驱动本地的docker拉取镜像

pipeline {
    agent {
        docker { image 'docker.io/beck123/locust:v1'}
    }
    
    stages {
        stage('Test') {
            steps {
                sh 'docker images'
            }
        }
    }
}

实际上发现build的console出现报错

Started by user admin
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /Users/beck/.jenkins/workspace/locust_test
[Pipeline] {
[Pipeline] isUnix
[Pipeline] withEnv
[Pipeline] {
[Pipeline] sh
+ docker inspect -f . docker.io/beck123/locust:v1
/Users/beck/.jenkins/workspace/locust_test@tmp/durable-884e1b18/script.sh: line 1: docker: command not found
[Pipeline] isUnix
[Pipeline] withEnv
[Pipeline] {
[Pipeline] sh
+ docker pull docker.io/beck123/locust:v1
/Users/beck/.jenkins/workspace/locust_test@tmp/durable-ef3eab12/script.sh: line 1: docker: command not found
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 127
Finished: FAILURE

解决办法

参考stack overflow上的这篇文章:docker command not found in local jenkins multi branch pipeline,里面说到

The solution is to add the following line into the /usr/local/Cellar/jenkins-lts/2.176.3/homebrew.mxcl.jenkins-lts.plist file so that Jenkins able to find the docker command from the host machine.

<key>EnvironmentVariables</key>
    <dict>
      <key>PATH</key>
      <string>/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/Docker.app/Contents/Resources/bin/:/Users/Kh0a/Library/Group\ Containers/group.com.docker/Applications/Docker.app/Contents/Resources/bin</string>
    </dict>

我本地的地址是:/usr/local/Cellar/jenkins-lts/2.346.3/homebrew.mxcl.jenkins-lts.plist,直接修改后然后brew services restart jenkins-lts即可

问题二:+ docker images /Users/beck/.jenkins/workspace/locust_test@tmp/durable-195d79d3/script.sh: 1: docker: not found [Pipeline] }

如果仔细看console的报错信息,就会发现这个命令是在容器内部执行的,容器内部没有安装docker,所以出错了

Started by user admin
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /Users/beck/.jenkins/workspace/locust_test
[Pipeline] {
[Pipeline] isUnix
[Pipeline] withEnv
[Pipeline] {
[Pipeline] sh
+ docker inspect -f . docker.io/beck123/locust:v1

Error: No such object: docker.io/beck123/locust:v1
[Pipeline] isUnix
[Pipeline] withEnv
[Pipeline] {
[Pipeline] sh
+ docker pull docker.io/beck123/locust:v1
v1: Pulling from beck123/locust
32de3c850997: Already exists
fa1d4c8d85a4: Already exists
c796299bbbdd: Already exists
81283a9569ad: Already exists
60b38700e7fb: Already exists
0f67f32c26d3: Already exists
e04be0d92e59: Already exists
b5052f164cdb: Already exists
e9c72aa5d062: Already exists
fa4d8dfca272: Already exists
3e38e9881935: Already exists
b2afa8015854: Already exists
Digest: sha256:75cd72c415c23fad7fd0bcab1397097ad49feea867376a68b61f502fb510af72
Status: Downloaded newer image for beck123/locust:v1
docker.io/beck123/locust:v1
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] **withDockerContainer**
Jenkins does not seem to be running inside a container
$ docker run -t -d -u 501:20 -w /Users/beck/.jenkins/workspace/locust_test -v /Users/beck/.jenkins/workspace/locust_test:/Users/beck/.jenkins/workspace/locust_test:rw,z -v /Users/beck/.jenkins/workspace/locust_test@tmp:/Users/beck/.jenkins/workspace/locust_test@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 ******** -e ******** docker.io/beck123/locust:v1 cat
$ docker top 62a42ab5460e7ebd731de892a43a982231272f764471ead0606ace2688b90bda -eo pid,comm
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Test)
[Pipeline] sh
+ docker images
/Users/beck/.jenkins/workspace/locust_test@tmp/durable-195d79d3/script.sh: 1: docker: not found
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
$ docker stop --time=1 62a42ab5460e7ebd731de892a43a982231272f764471ead0606ace2688b90bda
$ docker rm -f --volumes 62a42ab5460e7ebd731de892a43a982231272f764471ead0606ace2688b90bda
[Pipeline] // **withDockerContainer**
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 127
Finished: FAILURE

这时候可以输出下python的版本,可以看到成功了

pipeline {
    agent {
        docker { image 'docker.io/beck123/locust:v1'}
    }
    
    stages {
        stage('Test') {
            steps {
                sh '''
                    python --version
                   '''
            }
        }
    }
}
Jenkins does not seem to be running inside a container
$ docker run -t -d -u 501:20 -w /Users/beck/.jenkins/workspace/locust_test -v /Users/beck/.jenkins/workspace/locust_test:/Users/beck/.jenkins/workspace/locust_test:rw,z -v /Users/beck/.jenkins/workspace/locust_test@tmp:/Users/beck/.jenkins/workspace/locust_test@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 ******** -e ******** docker.io/beck123/locust:v1 cat
$ docker top a29a24779a958fbece3102def3f1bda29cf80f467672609f84273aae27c7b480 -eo pid,comm
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Test)
[Pipeline] sh
+ python --version
Python 3.8.16
+ ls -al
total 4
posted @ 2022-12-24 12:25  cnhkzyy  阅读(807)  评论(0编辑  收藏  举报