简体   繁体   中英

Jenkins docker agent in declarative pipeline: file not found

When trying to execute the example from https://github.com/jenkinsci/pipeline-model-definition-plugin/wiki/Controlling-your-build-environment :

pipeline {
    agent {
        docker 'node'
    }
    stages {
        stage("testing 123") {
            steps {
                sh 'node --version'
            }
        }
    }
}

I get the following output:

[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Declarative: Agent Setup)
[Pipeline] sh
[XXXXX-ABOESSRH5FV5AM3VLFMZ4UZP722N63WT5EEAE2JPIXC5U5ZTMBYA] Running shell script
+ docker pull node
Using default tag: latest
latest: Pulling from library/node
Digest: sha256:a72f8cd9aba12ea3a19ada91e077c4d8822d3bd7dc3c4707b16630e5c2477845
Status: Image is up to date for node:latest
[Pipeline] }
[Pipeline] // stage
[Pipeline] sh
[XXXXX-ABOESSRH5FV5AM3VLFMZ4UZP722N63WT5EEAE2JPIXC5U5ZTMBYA] Running shell script
+ docker inspect -f . node
.
[Pipeline] withDockerContainer
[Pipeline] // withDockerContainer
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline

GitHub has been notified of this commit’s build result

java.io.IOException: Cannot run program "docker": error=2, No such file or directory
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
    at hudson.Proc$LocalProc.<init>(Proc.java:245)
    at hudson.Proc$LocalProc.<init>(Proc.java:214)
    at hudson.Launcher$LocalLauncher.launch(Launcher.java:846)
    at hudson.Launcher$ProcStarter.start(Launcher.java:384)
    at org.jenkinsci.plugins.docker.workflow.client.DockerClient.launch(DockerClient.java:268)
    at org.jenkinsci.plugins.docker.workflow.client.DockerClient.launch(DockerClient.java:249)
    at org.jenkinsci.plugins.docker.workflow.client.DockerClient.launch(DockerClient.java:246)
    at org.jenkinsci.plugins.docker.workflow.client.DockerClient.version(DockerClient.java:218)
    at org.jenkinsci.plugins.docker.workflow.WithContainerStep$Execution.start(WithContainerStep.java:136)
    at org.jenkinsci.plugins.workflow.cps.DSL.invokeStep(DSL.java:184)
    at org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:126)
    at org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:108)
    at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:48)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
    at com.cloudbees.groovy.cps.sandbox.DefaultInvoker.methodCall(DefaultInvoker.java:18)
    at org.jenkinsci.plugins.docker.workflow.Docker$Image.inside(jar:file:/Users/Shared/Jenkins/Home/plugins/docker-workflow/WEB-INF/lib/docker-workflow.jar!/org/jenkinsci/plugins/docker/workflow/Docker.groovy:128)
    at org.jenkinsci.plugins.docker.workflow.Docker.node(jar:file:/Users/Shared/Jenkins/Home/plugins/docker-workflow/WEB-INF/lib/docker-workflow.jar!/org/jenkinsci/plugins/docker/workflow/Docker.groovy:63)
    at org.jenkinsci.plugins.docker.workflow.Docker$Image.inside(jar:file:/Users/Shared/Jenkins/Home/plugins/docker-workflow/WEB-INF/lib/docker-workflow.jar!/org/jenkinsci/plugins/docker/workflow/Docker.groovy:116)
    at org.jenkinsci.plugins.pipeline.modeldefinition.agent.impl.DockerPipelineScript.runImage(jar:file:/Users/Shared/Jenkins/Home/plugins/pipeline-model-definition/WEB-INF/lib/pipeline-model-definition.jar!/org/jenkinsci/plugins/pipeline/modeldefinition/agent/impl/DockerPipelineScript.groovy:54)
    at org.jenkinsci.plugins.pipeline.modeldefinition.agent.impl.AbstractDockerPipelineScript.configureRegistry(jar:file:/Users/Shared/Jenkins/Home/plugins/pipeline-model-definition/WEB-INF/lib/pipeline-model-definition.jar!/org/jenkinsci/plugins/pipeline/modeldefinition/agent/impl/AbstractDockerPipelineScript.groovy:68)
    at org.jenkinsci.plugins.pipeline.modeldefinition.agent.impl.AbstractDockerPipelineScript.run(jar:file:/Users/Shared/Jenkins/Home/plugins/pipeline-model-definition/WEB-INF/lib/pipeline-model-definition.jar!/org/jenkinsci/plugins/pipeline/modeldefinition/agent/impl/AbstractDockerPipelineScript.groovy:54)
    at org.jenkinsci.plugins.pipeline.modeldefinition.agent.CheckoutScript.checkoutAndRun(jar:file:/Users/Shared/Jenkins/Home/plugins/pipeline-model-extensions/WEB-INF/lib/pipeline-model-extensions.jar!/org/jenkinsci/plugins/pipeline/modeldefinition/agent/CheckoutScript.groovy:57)
    at org.jenkinsci.plugins.pipeline.modeldefinition.agent.CheckoutScript.doCheckout(jar:file:/Users/Shared/Jenkins/Home/plugins/pipeline-model-extensions/WEB-INF/lib/pipeline-model-extensions.jar!/org/jenkinsci/plugins/pipeline/modeldefinition/agent/CheckoutScript.groovy:40)
    at org.jenkinsci.plugins.pipeline.modeldefinition.agent.impl.LabelScript.run(jar:file:/Users/Shared/Jenkins/Home/plugins/pipeline-model-definition/WEB-INF/lib/pipeline-model-definition.jar!/org/jenkinsci/plugins/pipeline/modeldefinition/agent/impl/LabelScript.groovy:44)
    at ___cps.transform___(Native Method)
    at com.cloudbees.groovy.cps.impl.ContinuationGroup.methodCall(ContinuationGroup.java:57)
    at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:109)
    at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixArg(FunctionCallBlock.java:82)
    at sun.reflect.GeneratedMethodAccessor268.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
    at com.cloudbees.groovy.cps.impl.ClosureBlock.eval(ClosureBlock.java:46)
    at com.cloudbees.groovy.cps.Next.step(Next.java:74)
    at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:154)
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access$001(SandboxContinuable.java:18)
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable$1.call(SandboxContinuable.java:33)
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable$1.call(SandboxContinuable.java:30)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.GroovySandbox.runInSandbox(GroovySandbox.java:108)
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:30)
    at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:165)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:328)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$100(CpsThreadGroup.java:80)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:240)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:228)
    at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:64)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:112)
    at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.io.IOException: error=2, No such file or directory
    at java.lang.UNIXProcess.forkAndExec(Native Method)
    at java.lang.UNIXProcess.<init>(UNIXProcess.java:247)
    at java.lang.ProcessImpl.start(ProcessImpl.java:134)
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
    at hudson.Proc$LocalProc.<init>(Proc.java:245)
    at hudson.Proc$LocalProc.<init>(Proc.java:214)
    at hudson.Launcher$LocalLauncher.launch(Launcher.java:846)
    at hudson.Launcher$ProcStarter.start(Launcher.java:384)
    at org.jenkinsci.plugins.docker.workflow.client.DockerClient.launch(DockerClient.java:268)
    at org.jenkinsci.plugins.docker.workflow.client.DockerClient.launch(DockerClient.java:249)
    at org.jenkinsci.plugins.docker.workflow.client.DockerClient.launch(DockerClient.java:246)
    at org.jenkinsci.plugins.docker.workflow.client.DockerClient.version(DockerClient.java:218)
    at org.jenkinsci.plugins.docker.workflow.WithContainerStep$Execution.start(WithContainerStep.java:136)
    at org.jenkinsci.plugins.workflow.cps.DSL.invokeStep(DSL.java:184)
    at org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:126)
    at org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:108)
    at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:48)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
    at com.cloudbees.groovy.cps.sandbox.DefaultInvoker.methodCall(DefaultInvoker.java:18)
    ... 29 more
Finished: FAILURE

docker is installed on the server, and is in the PATH: the image is built :-) ( docker pull command succeed at the beginning of the pipeline)

I added a logger on org.jenkinsci.plugins.docker.workflow.client.DockerClient with the following result:

Apr 07, 2017 3:56:18 PM FINE org.jenkinsci.plugins.docker.workflow.client.DockerClient
Executing docker command docker -v

I also tried launching a docker in a stage with a regular agent, same issue:

pipeline {
  agent any
  stages {
    stage("testing 123") {
      agent {
        docker {
          image 'ubuntu:16.04'
        }
      }
      steps {
        sh 'uname -a'
      }
    }
  }
}

using Docker.Pipeline 1.10

This seems to be related to a PATH configuration issue on the node, but I can't find where.

If I run org.jenkinsci.plugins.docker.commons.tools.DockerTool.getExecutable("docker", Jenkins.getActiveInstance(), null, null) in the node console, I get the correct (existing, executable) docker path.

any idea ?

Thanks a lot!

You can run a shell command that is something like sh "docker 'node'"

What I've done is used a Jenkinsfile and Dockerfile (since I edit my images) and have the Jenkinsfile call a script that has

docker build --tag=image-name .
docker run --rm --name=image-name-container image-name \
./someScript.sh \
-j ${SCRIPT_PARAM1} \
-u ${SCRIPT_PARAM2} > ${DOCKER_OUTPUT_FILE_NAME}

inside of it.

The reason why you are facing this is that as per your Jenkins file you are not able to tell Jenkins that where node is? and that's why giving such exception,I can suggest some other way like but you can do is install node on your Jenkins master, configure the path in the configuration system by installing node plugin.Then, in the Jenkins file add below tools section at the top:

tools {
 node 'name-configured-in-the-configuration-system'
} 

In this way Jenkins will know that where your node is installed and would execute the command given in the stage.

To sperate stages and each stage its own container you can use the following style.

node() {
  checkout scm


  def String dockerImage = 'dockerImageName'
  def String dockerArgs = "-v ${WORKSPACE}/test:/tmp/test"

  stage ('Build') {
    docker.image(dockerImage).inside(dockerArgs) {
      echo 'Build them'
      }
    }

  stage('Test') {
    docker.image(dockerImage).inside(dockerArgs) {
      sh 'node --version'
    }

  }
}

If nodejs is installed in your docker image then without any issue it should work

Adding ~/.zshenv for zshell (or other file) to put docker in the path. I did this and problem resolved:

$ cat ~/.zshenv
export PATH=/usr/local/bin:$PATH

It looks like the docker inspect not run in the same env as docker pull .

On my Mac, the docker pull failed, then I configured node/agent to add such Name: PATH, Value: /usr/local/bin:$PATH to the Environment variables , then the docker pull will run normally. And docker inspect failed.

Then I add ~/.zshenv as above (and restart node/agent), the docker inspect worked finally.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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