如何解决错误:docker:守护进程的错误响应:工作目录无效

如何解决错误:docker:守护进程的错误响应:工作目录无效

我刚刚开始使用詹金斯DockerWindows10。目前我已经安装了詹金斯Docker并创造了一个Dockerfile和一个Jenkinsfile

不幸的是,每当我尝试建立詹金斯我一直收到错误。以下是 Jenkin 日志

Branch indexing
12:48:47 Connecting to https://api.github.com with no credentials, anonymous access
Obtained Jenkinsfile from 87252353bfae9210f07a234b6af7d17b6aee1363
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in C:\Users\oko\.jenkins\workspace\s-contest_ft-jenkins-integration
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Declarative: Checkout SCM)
[Pipeline] checkout
The recommended git tool is: NONE
No credentials specified
 > git.exe rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > git.exe config remote.origin.url https://github.com/FurahaSolutions/fs-online-maths-contest.git # timeout=10
Fetching without tags
Fetching upstream changes from https://github.com/FurahaSolutions/fs-online-maths-contest.git
 > git.exe --version # timeout=10
 > git --version # 'git version 2.23.0.windows.1'
 > git.exe fetch --no-tags --force --progress -- https://github.com/FurahaSolutions/fs-online-maths-contest.git +refs/heads/ft-jenkins-integration:refs/remotes/origin/ft-jenkins-integration # timeout=10
Checking out Revision 87252353bfae9210f07a234b6af7d17b6aee1363 (ft-jenkins-integration)
 > git.exe config core.sparsecheckout # timeout=10
 > git.exe checkout -f 87252353bfae9210f07a234b6af7d17b6aee1363 # timeout=10
Commit message: "Added Dockerfile"
 > git.exe rev-list --no-walk 036200096bf00516e0a8b149541ba994efcd360a # timeout=10
[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] isUnix
[Pipeline] bat

[m[32m]9;8;"USERNAME"\@]9;8;"COMPUTERNAME"\ [92mC:\Users\oko\.jenkins\workspace\s-contest_ft-jenkins-integration[90m
[90m>[m ]9;12\docker inspect -f . node:latest 
.
[Pipeline] withDockerContainer
Jenkins does not seem to be running inside a container
$ docker run -d -t -w C:/Users/oko/.jenkins/workspace/s-contest_ft-jenkins-integration/ -v C:/Users/oko/.jenkins/workspace/s-contest_ft-jenkins-integration/:C:/Users/oko/.jenkins/workspace/s-contest_ft-jenkins-integration/ -v C:/Users/oko/.jenkins/workspace/s-contest_ft-jenkins-integration@tmp/:C:/Users/oko/.jenkins/workspace/s-contest_ft-jenkins-integration@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 ******** node:latest cmd.exe
[Pipeline] // withDockerContainer
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
java.io.IOException: Failed to run image 'node:latest'. Error: docker: Error response from daemon: the working directory 'C:/Users/oko/.jenkins/workspace/s-contest_ft-jenkins-integration/' 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:198)
    at org.jenkinsci.plugins.workflow.cps.DSL.invokeStep(DSL.java:319)
    at org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:193)
    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(Docker.groovy:126)
    at org.jenkinsci.plugins.docker.workflow.Docker.node(Docker.groovy:66)
    at org.jenkinsci.plugins.docker.workflow.Docker$Image.inside(Docker.groovy:114)
    at org.jenkinsci.plugins.docker.workflow.declarative.DockerPipelineScript.runImage(DockerPipelineScript.groovy:57)
    at org.jenkinsci.plugins.docker.workflow.declarative.AbstractDockerPipelineScript.configureRegistry(AbstractDockerPipelineScript.groovy:73)
    at org.jenkinsci.plugins.docker.workflow.declarative.AbstractDockerPipelineScript.run(AbstractDockerPipelineScript.groovy:51)
    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:86)
    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 sun.reflect.GeneratedMethodAccessor487.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    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:129)
    at org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:268)
    at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:163)
    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:185)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:400)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$400(CpsThreadGroup.java:96)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:312)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:276)
    at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:67)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:139)
    at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
    at jenkins.security.ImpersonatingExecutorService$1.run(ImpersonatingExecutorService.java:68)
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Finished: FAILUREBranch indexing
12:48:47 Connecting to https://api.github.com with no credentials, anonymous access
Obtained Jenkinsfile from 87252353bfae9210f07a234b6af7d17b6aee1363
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in C:\Users\oko\.jenkins\workspace\s-contest_ft-jenkins-integration
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Declarative: Checkout SCM)
[Pipeline] checkout
The recommended git tool is: NONE
No credentials specified
 > git.exe rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > git.exe config remote.origin.url https://github.com/FurahaSolutions/fs-online-maths-contest.git # timeout=10
Fetching without tags
Fetching upstream changes from https://github.com/FurahaSolutions/fs-online-maths-contest.git
 > git.exe --version # timeout=10
 > git --version # 'git version 2.23.0.windows.1'
 > git.exe fetch --no-tags --force --progress -- https://github.com/FurahaSolutions/fs-online-maths-contest.git +refs/heads/ft-jenkins-integration:refs/remotes/origin/ft-jenkins-integration # timeout=10
Checking out Revision 87252353bfae9210f07a234b6af7d17b6aee1363 (ft-jenkins-integration)
 > git.exe config core.sparsecheckout # timeout=10
 > git.exe checkout -f 87252353bfae9210f07a234b6af7d17b6aee1363 # timeout=10
Commit message: "Added Dockerfile"
 > git.exe rev-list --no-walk 036200096bf00516e0a8b149541ba994efcd360a # timeout=10
[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] isUnix
[Pipeline] bat

[m[32m]9;8;"USERNAME"\@]9;8;"COMPUTERNAME"\ [92mC:\Users\oko\.jenkins\workspace\s-contest_ft-jenkins-integration[90m
[90m>[m ]9;12\docker inspect -f . node:latest 
.
[Pipeline] withDockerContainer
Jenkins does not seem to be running inside a container
$ docker run -d -t -w C:/Users/oko/.jenkins/workspace/s-contest_ft-jenkins-integration/ -v C:/Users/oko/.jenkins/workspace/s-contest_ft-jenkins-integration/:C:/Users/oko/.jenkins/workspace/s-contest_ft-jenkins-integration/ -v C:/Users/oko/.jenkins/workspace/s-contest_ft-jenkins-integration@tmp/:C:/Users/oko/.jenkins/workspace/s-contest_ft-jenkins-integration@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 ******** node:latest cmd.exe
[Pipeline] // withDockerContainer
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
java.io.IOException: Failed to run image 'node:latest'. Error: docker: Error response from daemon: the working directory 'C:/Users/oko/.jenkins/workspace/s-contest_ft-jenkins-integration/' 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:198)
    at org.jenkinsci.plugins.workflow.cps.DSL.invokeStep(DSL.java:319)
    at org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:193)
    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(Docker.groovy:126)
    at org.jenkinsci.plugins.docker.workflow.Docker.node(Docker.groovy:66)
    at org.jenkinsci.plugins.docker.workflow.Docker$Image.inside(Docker.groovy:114)
    at org.jenkinsci.plugins.docker.workflow.declarative.DockerPipelineScript.runImage(DockerPipelineScript.groovy:57)
    at org.jenkinsci.plugins.docker.workflow.declarative.AbstractDockerPipelineScript.configureRegistry(AbstractDockerPipelineScript.groovy:73)
    at org.jenkinsci.plugins.docker.workflow.declarative.AbstractDockerPipelineScript.run(AbstractDockerPipelineScript.groovy:51)
    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:86)
    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 sun.reflect.GeneratedMethodAccessor487.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    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:129)
    at org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:268)
    at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:163)
    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:185)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:400)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$400(CpsThreadGroup.java:96)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:312)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:276)
    at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:67)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:139)
    at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
    at jenkins.security.ImpersonatingExecutorService$1.run(ImpersonatingExecutorService.java:68)
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Finished: FAILURE

Dockerfile

# Stage 1

FROM node:10-alpine as build-step

RUN mkdir -p /app

WORKDIR /app

COPY package.json /app

RUN npm install

COPY . /app

RUN npm run build --prod


# Stage 2

FROM nginx:1.17.1-alpine

COPY --from=build-step /app/docs /usr/share/nginx/html

Jenkins文件

pipeline {
  agent {
    docker { image 'node:latest' }
  }
  stages {
    stage('Install') {
      steps { bat 'npm install' }
    }

    stage('Test') {
      parallel {
        stage('Static code analysis') {
            steps { bat 'npm run-script lint' }
        }
        stage('Unit tests') {
            steps { bat 'npm run-script test' }
        }
      }
    }

    stage('Build') {
      steps { bat 'npm run-script build' }
    }
  }
}

我正在构建使用以下方式构建的节点应用程序角度

答案1

您的 Dockerized Jenkins 实例正在尝试使用 Docker API 来启动一个新的 Docker 容器来进行构建。它想要与之通信的这个 Docker API 与 Jenkins 本身正在运行的 Docker 守护进程相同。

在 Linux 上运行的普通 Docker 守护程序上,您只需将 Docker 套接字作为/var/run/docker.sock卷传递给 Jenkins 容器(例如,-v /var/run/docker.sock:/var/run/docker.sock作为docker run命令的参数)。

但是,在适用于 Windows 的 Docker 上,您正在 Windows 上的 Linux VM 中运行 Docker(通过 WSL2 或 Hyper-V)。根据您设置 Docker 环境的方式,您可以尝试使用以下方法之一在 Jenkins 容器中绑定 Docker 套接字:

  • -v /var/run/docker.sock:/var/run/docker.sock
  • -v //var/run/docker.sock:/var/run/docker.sock
  • -v tcp://127.0.0.1:2376:/var/run/docker.sock

我已经看到一些解决方案除了上述方法之外还这样做,以便对 Windows 版 Docker 进行一些黑客攻击:

  • -v /usr/local/bin/docker:/usr/bin/docker

或者,一个更好的解决方案,需要更少的黑客:在正确安装的 Linux 操作系统中执行此操作。不应在生产环境中使用适用于 Windows 的 Docker。

相关内容