Jenkins:无法运行程序“docker”:error = 2,没有这样的文件或目录

时间:2018-05-14 14:53:55

标签: java docker jenkins

我已在我的本地计算机上安装了Jenkins,该计算机在MacOS High Sierra上运行并安装了docker。我正在尝试运行一个使用docker的简单管道示例。我已将以下行添加到管道中:

pipeline {
    agent {
        docker { image 'node:7-alpine' }
    }
    stages {
        stage('Test') {
            steps {
                sh 'node --version'
            }
        }
    }
}

然后从WebGUI,我现在点击构建,它失败了。控制台输出显示错误如下:

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:249)
 at hudson.Proc$LocalProc.<init>(Proc.java:218)
 at hudson.Launcher$LocalLauncher.launch(Launcher.java:929)
 at hudson.Launcher$ProcStarter.start(Launcher.java:449)
 at org.jenkinsci.plugins.docker.workflow.client.DockerClient.launch(DockerClient.java:296)
 at org.jenkinsci.plugins.docker.workflow.client.DockerClient.launch(DockerClient.java:277)
 at org.jenkinsci.plugins.docker.workflow.client.DockerClient.launch(DockerClient.java:274)
 at org.jenkinsci.plugins.docker.workflow.client.DockerClient.version(DockerClient.java:246)
 at org.jenkinsci.plugins.docker.workflow.WithContainerStep$Execution.start(WithContainerStep.java:141)
 at org.jenkinsci.plugins.workflow.cps.DSL.invokeStep(DSL.java:229)
 at org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:153)
 at org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:122)
 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:20)
Caused: 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:249)
 at hudson.Proc$LocalProc.<init>(Proc.java:218)
 at hudson.Launcher$LocalLauncher.launch(Launcher.java:929)
 at hudson.Launcher$ProcStarter.start(Launcher.java:449)
 at org.jenkinsci.plugins.docker.workflow.client.DockerClient.launch(DockerClient.java:296)
 at org.jenkinsci.plugins.docker.workflow.client.DockerClient.launch(DockerClient.java:277)
 at org.jenkinsci.plugins.docker.workflow.client.DockerClient.launch(DockerClient.java:274)
 at org.jenkinsci.plugins.docker.workflow.client.DockerClient.version(DockerClient.java:246)
 at org.jenkinsci.plugins.docker.workflow.WithContainerStep$Execution.start(WithContainerStep.java:141)
 at org.jenkinsci.plugins.workflow.cps.DSL.invokeStep(DSL.java:229)
 at org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:153)
 at org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:122)
 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:20)
 at org.jenkinsci.plugins.docker.workflow.Docker$Image.inside(jar:file:/Users/coder/.jenkins/plugins/docker-workflow/WEB-INF/lib/docker-workflow.jar!/org/jenkinsci/plugins/docker/workflow/Docker.groovy:135)
 at org.jenkinsci.plugins.docker.workflow.Docker.node(jar:file:/Users/coder/.jenkins/plugins/docker-workflow/WEB-INF/lib/docker-workflow.jar!/org/jenkinsci/plugins/docker/workflow/Docker.groovy:66)
 at org.jenkinsci.plugins.docker.workflow.Docker$Image.inside(jar:file:/Users/coder/.jenkins/plugins/docker-workflow/WEB-INF/lib/docker-workflow.jar!/org/jenkinsci/plugins/docker/workflow/Docker.groovy:123)
 at org.jenkinsci.plugins.pipeline.modeldefinition.agent.impl.DockerPipelineScript.runImage(jar:file:/Users/coder/.jenkins/plugins/pipeline-model-definition/WEB-INF/lib/pipeline-model-definition.jar!/org/jenkinsci/plugins/pipeline/modeldefinition/agent/impl/DockerPipelineScript.groovy:57)
 at org.jenkinsci.plugins.pipeline.modeldefinition.agent.impl.AbstractDockerPipelineScript.configureRegistry(jar:file:/Users/coder/.jenkins/plugins/pipeline-model-definition/WEB-INF/lib/pipeline-model-definition.jar!/org/jenkinsci/plugins/pipeline/modeldefinition/agent/impl/AbstractDockerPipelineScript.groovy:74)
 at org.jenkinsci.plugins.pipeline.modeldefinition.agent.impl.AbstractDockerPipelineScript.run(jar:file:/Users/coder/.jenkins/plugins/pipeline-model-definition/WEB-INF/lib/pipeline-model-definition.jar!/org/jenkinsci/plugins/pipeline/modeldefinition/agent/impl/AbstractDockerPipelineScript.groovy:52)
 at org.jenkinsci.plugins.pipeline.modeldefinition.agent.CheckoutScript.checkoutAndRun(jar:file:/Users/coder/.jenkins/plugins/pipeline-model-extensions/WEB-INF/lib/pipeline-model-extensions.jar!/org/jenkinsci/plugins/pipeline/modeldefinition/agent/CheckoutScript.groovy:66)
 at org.jenkinsci.plugins.pipeline.modeldefinition.agent.CheckoutScript.doCheckout(jar:file:/Users/coder/.jenkins/plugins/pipeline-model-extensions/WEB-INF/lib/pipeline-model-extensions.jar!/org/jenkinsci/plugins/pipeline/modeldefinition/agent/CheckoutScript.groovy:42)
 at org.jenkinsci.plugins.pipeline.modeldefinition.agent.impl.LabelScript.run(jar:file:/Users/coder/.jenkins/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.GeneratedMethodAccessor192.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:83)
 at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:174)
 at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:163)
 at org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.use(GroovyCategorySupport.java:122)
 at org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:261)
 at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:163)
 at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access$101(SandboxContinuable.java:34)
 at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.lambda$run0$0(SandboxContinuable.java:59)
 at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.GroovySandbox.runInSandbox(GroovySandbox.java:108)
 at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:58)
 at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:174)
 at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:332)
 at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$200(CpsThreadGroup.java:83)
 at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:244)
 at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:232)
 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:131)
 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)
Finished: FAILURE

为什么会这样?在Jenkins文档中,它们仅提供此语法,并未提及任何其他内容。

3 个答案:

答案 0 :(得分:7)

我遇到了与上述相同的问题。我不确定我的情况与您的情况完全相同,但TL; DR是否需要确保Docker对Jenkins正在使用的PATH变量可用。具体来说就是它的启动方式,可以在 Jenkins主页->管理Jenkins->系统信息下看到。

就我而言,我在Mac上,并通过Homebrew安装了Jenkins。为了解决此问题,我编辑了/usr/local/opt/jenkins-lts/homebrew.mxcl.jenkins-lts.plist文件,并将其更改为以下内容:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>Label</key>
    <string>homebrew.mxcl.jenkins-lts</string>
    <key>ProgramArguments</key>
    <array>
      <string>/usr/libexec/java_home</string>
      <string>-v</string>
      <string>1.8</string>
      <string>--exec</string>
      <string>java</string>
      <string>-Dmail.smtp.starttls.enable=true</string>
      <string>-jar</string>
      <string>/usr/local/opt/jenkins-lts/libexec/jenkins.war</string>
      <string>--httpListenAddress=127.0.0.1</string>
      <string>--httpPort=8080</string>
    </array>
    <key>EnvironmentVariables</key>
    <dict>
      <key>PATH</key>
      <string>/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin</string>
    </dict>
    <key>RunAtLoad</key>
    <true/>
  </dict>
</plist>

这里的重要部分是:

    <key>EnvironmentVariables</key>
    <dict>
      <key>PATH</key>
      <string>/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin</string>
    </dict>

就我而言,docker/usr/local/bin可用。

答案 1 :(得分:4)

我最近在 macOS 上遇到了同样的问题。通过在 /usr/local/opt/jenkins-lts/homebrew.mxcl.jenkins-lts.plist

中添加以下几行来解决
    <key>EnvironmentVariables</key>
    <dict>
      <key>PATH</key>
      <string>/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin</string>
    </dict>

它还需要重启 jenkins brew services restart jenkins-lts

顺便说一下,在 jenkins web GUI 中添加 PATH 环境并不能解决问题。它在某些部分起作用,并且整个过程都失败了,这使它更加困惑并误导了我太多。

答案 2 :(得分:1)

我对此也遇到了严重的麻烦,所以我想发布我的解决方案,因为我在任何地方都没有看到它。

我的设置是:运行Mac OS Sierra的Mac Mini,jenkins和docker均安装在本地。 Jenkins无法在groovy脚本中调用docker,但是shell可以访问docker命令。

解决方案:

  1. 管理Jenkins>全局工具配置
  2. 单击以添加docker安装
  3. 随意命名,您将使用该名称在Groovy脚本中标识安装
  4. 将安装根目录设置为Docker的安装位置(默认情况下,该目录位于/ usr / local中)。如果我附加/ bin这个路径,这对我不起作用,因为我在Jenkins中使用的docker插件会自动执行此操作。我们窥探了docker-workflow-plugin的源代码,以进行验证。
  5. 保存这些更改
  6. 打开您要在其中调用docker安装的Groovy脚本,并将docker调用包含在如下语句中:

    docker.withTool(''){    //无论您希望在此处运行的任何docker命令 }