简体   繁体   English

声明性管道中的 Jenkins docker 代理:找不到文件

[英]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 :当尝试从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) docker 安装在服务器上,并且在 PATH 中:镜像构建:-)(管道开始时docker pull命令成功)

I added a logger on org.jenkinsci.plugins.docker.workflow.client.DockerClient with the following result:我在org.jenkinsci.plugins.docker.workflow.client.DockerClient上添加了一个记录器,结果如下:

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:我还尝试在具有常规代理的阶段启动 docker,同样的问题:

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

using Docker.Pipeline 1.10使用 Docker.Pipeline 1.10

This seems to be related to a PATH configuration issue on the node, but I can't find where.这似乎与节点上的 PATH 配置问题有关,但我找不到在哪里。

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.如果我在节点控制台中运行org.jenkinsci.plugins.docker.commons.tools.DockerTool.getExecutable("docker", Jenkins.getActiveInstance(), null, null) ,我会得到正确的(现有的、可执行的)docker 路径。

any idea ?任何的想法 ?

Thanks a lot!非常感谢!

You can run a shell command that is something like sh "docker 'node'"您可以运行类似于sh "docker 'node'"的 shell 命令

What I've done is used a Jenkinsfile and Dockerfile (since I edit my images) and have the Jenkinsfile call a script that has我所做的是使用 Jenkinsfile 和 Dockerfile(因为我编辑了我的图像)并让 Jenkinsfile 调用了一个脚本

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?您面临这种情况的原因是,根据您的 Jenkins 文件,您无法告诉 Jenkins 节点在哪里? 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:这就是为什么给出这样的例外,我可以建议一些其他的方式,但你可以做的是在你的 Jenkins 主节点上安装节点,通过安装节点插件在配置系统中配置路径。然后,在 Jenkins 文件中添加下面的工具部分最佳:

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.通过这种方式,Jenkins 将知道您的节点安装在哪里,并会执行阶段中给出的命令。

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如果您的 docker 镜像中安装了 nodejs,那么它应该可以正常工作

Adding ~/.zshenv for zshell (or other file) to put docker in the path.为 zshell(或其他文件)添加~/.zshenv以将 docker 放入路径中。 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 .看起来docker inspect没有在与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.在我的 Mac 上, docker pull失败,然后我配置 node/agent 在Environment variables添加这样的Name: PATH, Value: /usr/local/bin:$PATH ,然后docker pull将正常运行。 And docker inspect failed.并且docker inspect失败。

Then I add ~/.zshenv as above (and restart node/agent), the docker inspect worked finally.然后我添加~/.zshenv如上所述(并重新启动节点/代理), ~/.zshenv docker inspect终于工作了。

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

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