如何修复Jenkins(在Windows上)中的Docker错误?

ivqmmu1c  于 2023-01-20  发布在  Docker
关注(0)|答案(1)|浏览(146)

我正试图进入Jenkins,但我现在被困在教程https://www.jenkins.io/doc/pipeline/tour/hello-world/的第一步几个小时。
我按照说明在Windows机器上安装了JDK和Docker Desktop。对于Docker Desktop,我还需要用Ubuntu安装WSL 2。我创建了一个示例GitHub Repo,其中只有一个Jenkinsfile,包含教程中的示例代码:

/* Requires the Docker Pipeline plugin */
pipeline {
    agent { docker { image 'python:3.10.7-alpine' } }
    stages {
        stage('build') {
            steps {
                sh 'python --version'
            }
        }
    }
}

如果我启动一个连接到GitHub仓库的Jenkins管道并试图构建它,我会得到以下错误:

13:01:46 Connecting to https://api.github.com with no credentials, anonymous access
Obtained Jenkinsfile from 7533f199524c45c20290f15d17d103958b24abc3
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in C:\Users\wenni\.jenkins\workspace\FirstPipeline_main
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Declarative: Checkout SCM)
[Pipeline] checkout
The recommended git tool is: NONE
No credentials specified
 > C:\Program Files\Git\cmd\git.exe rev-parse --resolve-git-dir C:\Users\wenni\.jenkins\workspace\FirstPipeline_main\.git # timeout=10
Fetching changes from the remote Git repository
 > C:\Program Files\Git\cmd\git.exe config remote.origin.url https://github.com/Wagnerd6/JenkinsPipelineTest.git # timeout=10
Fetching without tags
Fetching upstream changes from https://github.com/Wagnerd6/JenkinsPipelineTest.git
 > C:\Program Files\Git\cmd\git.exe --version # timeout=10
 > git --version # 'git version 2.38.1.windows.1'
 > C:\Program Files\Git\cmd\git.exe fetch --no-tags --force --progress -- https://github.com/Wagnerd6/JenkinsPipelineTest.git +refs/heads/main:refs/remotes/origin/main # timeout=10
Checking out Revision 7533f199524c45c20290f15d17d103958b24abc3 (main)
 > C:\Program Files\Git\cmd\git.exe config core.sparsecheckout # timeout=10
 > C:\Program Files\Git\cmd\git.exe checkout -f 7533f199524c45c20290f15d17d103958b24abc3 # timeout=10
Commit message: "Python docker img"
 > C:\Program Files\Git\cmd\git.exe rev-list --no-walk e7de46ec1626389bb4e5c5577013ebaa8f214de2 # timeout=10
[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] isUnix
[Pipeline] withEnv
[Pipeline] {
[Pipeline] bat

C:\Users\wenni\.jenkins\workspace\FirstPipeline_main>docker inspect -f . "python:3.10.7-alpine" 
.
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] withDockerContainer
Jenkins does not seem to be running inside a container
$ docker run -d -t -w C:/Users/wenni/.jenkins/workspace/FirstPipeline_main/ -v C:/Users/wenni/.jenkins/workspace/FirstPipeline_main/:C:/Users/wenni/.jenkins/workspace/FirstPipeline_main/ -v C:/Users/wenni/.jenkins/workspace/FirstPipeline_main@tmp/:C:/Users/wenni/.jenkins/workspace/FirstPipeline_main@tmp/ -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 ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** python:3.10.7-alpine cmd.exe
[Pipeline] // withDockerContainer
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
java.io.IOException: Failed to run image 'python:3.10.7-alpine'. Error: docker: Error response from daemon: the working directory 'C:/Users/wenni/.jenkins/workspace/FirstPipeline_main/' is invalid, it needs to be an absolute path.
See 'docker run --help'.
    at org.jenkinsci.plugins.docker.workflow.client.WindowsDockerClient.run(WindowsDockerClient.java:58)
    at org.jenkinsci.plugins.docker.workflow.WithContainerStep$Execution.start(WithContainerStep.java:200)
    at org.jenkinsci.plugins.workflow.cps.DSL.invokeStep(DSL.java:322)
    at org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:196)
    at org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:124)
    at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:47)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
    at com.cloudbees.groovy.cps.sandbox.DefaultInvoker.methodCall(DefaultInvoker.java:20)
    at org.jenkinsci.plugins.docker.workflow.Docker$Image.inside(Docker.groovy:140)
    at org.jenkinsci.plugins.docker.workflow.Docker.node(Docker.groovy:66)
    at org.jenkinsci.plugins.docker.workflow.Docker$Image.inside(Docker.groovy:125)
    at org.jenkinsci.plugins.docker.workflow.declarative.DockerPipelineScript.runImage(DockerPipelineScript.groovy:54)
    at org.jenkinsci.plugins.docker.workflow.declarative.AbstractDockerPipelineScript.configureRegistry(AbstractDockerPipelineScript.groovy:63)
    at org.jenkinsci.plugins.docker.workflow.declarative.AbstractDockerPipelineScript.run(AbstractDockerPipelineScript.groovy:50)
    at org.jenkinsci.plugins.pipeline.modeldefinition.agent.CheckoutScript.checkoutAndRun(CheckoutScript.groovy:61)
    at ___cps.transform___(Native Method)
    at com.cloudbees.groovy.cps.impl.ContinuationGroup.methodCall(ContinuationGroup.java:90)
    at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:113)
    at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixArg(FunctionCallBlock.java:83)
    at jdk.internal.reflect.GeneratedMethodAccessor125.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    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:177)
    at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:166)
    at org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.use(GroovyCategorySupport.java:136)
    at org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:275)
    at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:166)
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access$001(SandboxContinuable.java:18)
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:51)
    at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:187)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:420)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$400(CpsThreadGroup.java:95)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:330)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:294)
    at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:67)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:139)
    at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:30)
    at jenkins.security.ImpersonatingExecutorService$1.run(ImpersonatingExecutorService.java:70)
    at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
    at java.base/java.lang.Thread.run(Thread.java:833)
Finished: FAILURE

当我用agent any替换agent { docker { image 'python:3.10.7-alpine' } }时,构建工作正常,但在python上当然失败了--因为Python版本没有安装。
我的研究只是让我发现Windows和运行Docker容器存在问题,难道真的不能在Windows机器上简单地遵循Jenkins的第一个教程页面吗?

nvbavucw

nvbavucw1#

遗憾的是,代理“docker“在Windows托管的Linux环境中不兼容。
这是一个已知的问题安静了一段时间,任何努力,使一个持久的修复失败。
主要问题是Windows和Linux文件路径之间的不兼容。因此经常报告错误:

"Error: docker: Error response from daemon: the working directory 'C:/Jenkins/workspace/testspace/' is invalid, it needs to be an absolute path."

快速学习Jenkins教程的唯一方法是使用Docker CLI(命令行)命令。继续学习教程的基本示例如下所示:

pipeline {
agent any
    stages {
        stage('build') {
            steps {
                script {
                   /* the return value gets caught and saved into the variable MY_CONTAINER */
                   MY_CONTAINER = bat(script: '@docker run -d -i python:3.10.7-alpine', returnStdout: true).trim()
                   echo "mycontainer_id is ${MY_CONTAINER}"
                   /* python --version gets executed inside the Container */
                   bat "docker exec ${MY_CONTAINER} python --version "
                   /* the Container gets removed */
                   bat "docker rm -f ${MY_CONTAINER}"
                        }
                    }
                }
            }
        }

与Jenkins教程脚本相比,有两个主要更改:
1.基于Windows的系统应该使用bat而不是sh来执行批处理命令。
1.避免使用代理“docker“并将其替换为any

相关问题