Answers: <p>I could not achieve changing the working directory using the suggestion by Vishwas, although I did try creating a Jenkins user inside the container with the same uid as the host. It just didn't work for me. I did however, realize that I didn't actually need to change the directory on the container because the workspace was already in the container.</p>
  <p>I'm using the Docker Plugin from CloudBees. After reading their documentation, I realized the workstation folder I thought was on the host machine, was actually in the container. Part of the documentation stated:</p>
  <blockquote>
  <p>The above is a complete Pipeline script. inside will:</p>
  <ol>
  <li>Automatically grab an agent and a workspace (no extra node block is required).</li>
  <li>Pull the requested image to the Docker server (if not already cached).</li>
  <li>Start a container running that image.</li>
  <li><strong>Mount the Jenkins workspace as a &quot;volume&quot; inside the container, using the same file path.</strong></li>
  </ol>
  </blockquote>
  <p>After seeing item number four, I ran <code>docker container inspect &lt;containerID&gt;</code> and I saw the workspace <code>/home/jenkins/workspace</code> was actually a volume mount that was mapped to the <code>/home/jenkins/workspace</code> on the Docker host machine as well. Using this as my workspace, I was able to build the various applications within the container and then moved the results to a second volume used for application collection later in the pipeline. It's not an exact answer to my original question, but it works!</p>
  
Answers: CC BY-SA 4.0
Answers: 2018-10-01T19:39:52.103Z
Answers: 2018-10-01T19:39:52.103Z
Answers: 2020-06-18T08:28:10.803Z
Answers: Argyle
Answers: <p>If your master is running as Jenkins user, you should make a user in the docker image which you use as to create your node and do the key exchange. For example <code>jenkins</code>user with same UID needs to exist on both master and node docker container.</p>
  
Answers: CC BY-SA 4.0
Answers: 2018-09-29T23:22:34.793Z
Answers: For example jenkins user with same uid need to exist on bot master and node docker conatiner.
Answers: vishwas gautam
Answers: CC BY-SA 4.0
Answers: 2018-09-30T00:16:34.580Z
Answers: Hi Vishwas, welcome to DevOps.SE.  You can edit your question to add more information like this.  I've submitted an edit to do this for you, but in the future you can do it.
Answers: chicks
Answers: CC BY-SA 4.0
Answers: 2018-09-29T23:21:50.017Z
Answers: 2018-09-30T23:21:00.510Z
Answers: 2018-09-30T23:21:00.510Z
Answers: vishwas gautam
Body: <p>Here's my current set-up:</p>
  
  <ol>
  <li>Jenkins Master running in Docker on an Ubuntu 18.04 host.</li>
  <li>A Node running Ubuntu 18.04 with Docker installed.</li>
  <li>Desired job spins up docker containers on the node which build products in parallel and output to a shared volume.</li>
  </ol>
  
  <p>The issue I'm running into is I'm unable to switch to a directory on the container in which I need to run build commands. The directories exist and can be access using <code>sh()</code> to run various commands for verification. When I try:</p>
  
  <pre><code>dir("/path/on/container") {
      sh "pwd"
  }
  </code></pre>
  
  <p>The console output appears that the <code>dir()</code> function is executed, but there's a failure when tryining to run anything between the curly braces. The failures are (full error text at the end of this post): <code>hudson.remoting.Channel$CallSiteStackTrace: Remote call to DockerHostNode</code> and <code>java.nio.file.AccessDeniedException: /path/on/container</code>.</p>
  
  <pre><code>[DockerTest] Running shell script
  + pwd
  /home/jenkins/workspace/DockerTest
  [Pipeline] dir
  Running in /path/on/container
  [Pipeline] {
  [Pipeline] echo
  Are we in the project dir?
  [Pipeline] sh
  [Pipeline] }
  [Pipeline] // dir
  [Pipeline] }
  Also:   hudson.remoting.Channel$CallSiteStackTrace: Remote call to DockerHostNode
          at hudson.remoting.Channel.attachCallSiteStackTrace(Channel.java:1741)
          at hudson.remoting.UserRequest$ExceptionResponse.retrieve(UserRequest.java:357)
          at hudson.remoting.Channel.call(Channel.java:955)
          at hudson.FilePath.act(FilePath.java:1070)
          at hudson.FilePath.act(FilePath.java:1059)
          at hudson.FilePath.mkdirs(FilePath.java:1244)
          at org.jenkinsci.plugins.durabletask.FileMonitoringTask$FileMonitoringController.&lt;init&gt;(FileMonitoringTask.java:171)
          at org.jenkinsci.plugins.durabletask.BourneShellScript$ShellController.&lt;init&gt;(BourneShellScript.java:197)
          at org.jenkinsci.plugins.durabletask.BourneShellScript$ShellController.&lt;init&gt;(BourneShellScript.java:189)
          at org.jenkinsci.plugins.durabletask.BourneShellScript.launchWithCookie(BourneShellScript.java:110)
          at org.jenkinsci.plugins.durabletask.FileMonitoringTask.launch(FileMonitoringTask.java:98)
          at org.jenkinsci.plugins.workflow.steps.durable_task.DurableTaskStep$Execution.start(DurableTaskStep.java:264)
          at org.jenkinsci.plugins.workflow.cps.DSL.invokeStep(DSL.java:270)
          at org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:180)
          at org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:122)
          at sun.reflect.GeneratedMethodAccessor263.invoke(Unknown Source)
          at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
          at java.lang.reflect.Method.invoke(Method.java:498)
          at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
          at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
          at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1213)
          at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1022)
          at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:42)
          at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
          at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
          at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:157)
          at org.kohsuke.groovy.sandbox.GroovyInterceptor.onMethodCall(GroovyInterceptor.java:23)
          at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:155)
          at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:155)
          at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:159)
          at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:129)
          at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:129)
          at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.methodCall(SandboxInvoker.java:17)
          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.GeneratedMethodAccessor157.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.ConstantBlock.eval(ConstantBlock.java:21)
          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$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:182)
          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 jenkins.security.ImpersonatingExecutorService$1.run(ImpersonatingExecutorService.java:59)
          at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
  java.nio.file.AccessDeniedException: /path/on/container
      at sun.nio.fs.UnixException.translateToIOException(UnixException.java:84)
      at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102)
      at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:107)
      at sun.nio.fs.UnixFileSystemProvider.createDirectory(UnixFileSystemProvider.java:384)
      at java.nio.file.Files.createDirectory(Files.java:674)
      at java.nio.file.Files.createAndCheckIsDirectory(Files.java:781)
      at java.nio.file.Files.createDirectories(Files.java:767)
      at hudson.FilePath.mkdirs(FilePath.java:3271)
      at hudson.FilePath.access$1300(FilePath.java:211)
      at hudson.FilePath$Mkdirs.invoke(FilePath.java:1252)
      at hudson.FilePath$Mkdirs.invoke(FilePath.java:1248)
      at hudson.FilePath$FileCallableWrapper.call(FilePath.java:3084)
      at hudson.remoting.UserRequest.perform(UserRequest.java:212)
      at hudson.remoting.UserRequest.perform(UserRequest.java:54)
      at hudson.remoting.Request$2.run(Request.java:369)
      at hudson.remoting.InterceptingExecutorService$1.call(InterceptingExecutorService.java:72)
      at java.util.concurrent.FutureTask.run(FutureTask.java:266)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
      at java.lang.Thread.run(Thread.java:748)
  </code></pre>
  
  <p>The path on the container is a docker volume and the containers are all running with -u root, so I'm not entirely sure why I'm getting an AccessDenied. The only thing I can think is that, because of the previous "Remote call to DockerHostNode", it's trying to access <code>/path/on/container</code> which doesn't exist of the node itself. Any ideads? Here's the full error text:</p>
  
Comments: CC BY-SA 4.0
Comments: 2020-08-12T02:07:11.833Z
Comments: This has been fixed on recent Jenkins versions. See date of this comment.
Comments: Lee Meador
Tags: <docker><jenkins>
Title: Is there an alternative to the Jenkins dir() to change to a directory on a Docker container?
UserDisplayName: Argyle
ThreadText: # Is there an alternative to the Jenkins dir() to change to a directory on a Docker container?
  
  (Asked by: username_0 on 2018-09-28)
  
  Here's my current set-up:
  
  1. Jenkins Master running in Docker on an Ubuntu 18.04 host.
  2. A Node running Ubuntu 18.04 with Docker installed.
  3. Desired job spins up docker containers on the node which build products in parallel and output to a shared volume.
  
  The issue I'm running into is I'm unable to switch to a directory on the container in which I need to run build commands. The directories exist and can be access using `sh()` to run various commands for verification. When I try:
  
  ```
  dir("/path/on/container") {
      sh "pwd"
  }
  
  ```
  
  The console output appears that the `dir()` function is executed, but there's a failure when tryining to run anything between the curly braces. The failures are (full error text at the end of this post): `hudson.remoting.Channel$CallSiteStackTrace: Remote call to DockerHostNode` and `java.nio.file.AccessDeniedException: /path/on/container`.
  
  ```
  [DockerTest] Running shell script
  + pwd
  /home/jenkins/workspace/DockerTest
  [Pipeline] dir
  Running in /path/on/container
  [Pipeline] {
  [Pipeline] echo
  Are we in the project dir?
  [Pipeline] sh
  [Pipeline] }
  [Pipeline] // dir
  [Pipeline] }
  Also:   hudson.remoting.Channel$CallSiteStackTrace: Remote call to DockerHostNode
          at hudson.remoting.Channel.attachCallSiteStackTrace(Channel.java:1741)
          at hudson.remoting.UserRequest$ExceptionResponse.retrieve(UserRequest.java:357)
          at hudson.remoting.Channel.call(Channel.java:955)
          at hudson.FilePath.act(FilePath.java:1070)
          at hudson.FilePath.act(FilePath.java:1059)
          at hudson.FilePath.mkdirs(FilePath.java:1244)
          at org.jenkinsci.plugins.durabletask.FileMonitoringTask$FileMonitoringController.<init>(FileMonitoringTask.java:171)
          at org.jenkinsci.plugins.durabletask.BourneShellScript$ShellController.<init>(BourneShellScript.java:197)
          at org.jenkinsci.plugins.durabletask.BourneShellScript$ShellController.<init>(BourneShellScript.java:189)
          at org.jenkinsci.plugins.durabletask.BourneShellScript.launchWithCookie(BourneShellScript.java:110)
          at org.jenkinsci.plugins.durabletask.FileMonitoringTask.launch(FileMonitoringTask.java:98)
          at org.jenkinsci.plugins.workflow.steps.durable_task.DurableTaskStep$Execution.start(DurableTaskStep.java:264)
          at org.jenkinsci.plugins.workflow.cps.DSL.invokeStep(DSL.java:270)
          at org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:180)
          at org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:122)
          at sun.reflect.GeneratedMethodAccessor263.invoke(Unknown Source)
          at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
          at java.lang.reflect.Method.invoke(Method.java:498)
          at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
          at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
          at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1213)
          at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1022)
          at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:42)
          at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
          at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
          at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:157)
          at org.kohsuke.groovy.sandbox.GroovyInterceptor.onMethodCall(GroovyInterceptor.java:23)
          at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:155)
          at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:155)
          at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:159)
          at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:129)
          at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:129)
          at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.methodCall(SandboxInvoker.java:17)
          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.GeneratedMethodAccessor157.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.ConstantBlock.eval(ConstantBlock.java:21)
          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$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:182)
          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 jenkins.security.ImpersonatingExecutorService$1.run(ImpersonatingExecutorService.java:59)
          at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
  java.nio.file.AccessDeniedException: /path/on/container
      at sun.nio.fs.UnixException.translateToIOException(UnixException.java:84)
      at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102)
      at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:107)
      at sun.nio.fs.UnixFileSystemProvider.createDirectory(UnixFileSystemProvider.java:384)
      at java.nio.file.Files.createDirectory(Files.java:674)
      at java.nio.file.Files.createAndCheckIsDirectory(Files.java:781)
      at java.nio.file.Files.createDirectories(Files.java:767)
      at hudson.FilePath.mkdirs(FilePath.java:3271)
      at hudson.FilePath.access$1300(FilePath.java:211)
      at hudson.FilePath$Mkdirs.invoke(FilePath.java:1252)
      at hudson.FilePath$Mkdirs.invoke(FilePath.java:1248)
      at hudson.FilePath$FileCallableWrapper.call(FilePath.java:3084)
      at hudson.remoting.UserRequest.perform(UserRequest.java:212)
      at hudson.remoting.UserRequest.perform(UserRequest.java:54)
      at hudson.remoting.Request$2.run(Request.java:369)
      at hudson.remoting.InterceptingExecutorService$1.call(InterceptingExecutorService.java:72)
      at java.util.concurrent.FutureTask.run(FutureTask.java:266)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
      at java.lang.Thread.run(Thread.java:748)
  
  ```
  
  The path on the container is a docker volume and the containers are all running with -u root, so I'm not entirely sure why I'm getting an AccessDenied. The only thing I can think is that, because of the previous "Remote call to DockerHostNode", it's trying to access `/path/on/container` which doesn't exist of the node itself. Any ideads? Here's the full error text:
  
  **Tags:** docker, jenkins
  
  > **Comments:**
  > **username_1**: This has been fixed on recent Jenkins versions. See date of this comment.  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_0 on 2018-10-01. Score: 2 | Accepted answer
  
  I could not achieve changing the working directory using the suggestion by username_2, although I did try creating a Jenkins user inside the container with the same uid as the host. It just didn't work for me. I did however, realize that I didn't actually need to change the directory on the container because the workspace was already in the container.
  
  I'm using the Docker Plugin from CloudBees. After reading their documentation, I realized the workstation folder I thought was on the host machine, was actually in the container. Part of the documentation stated:
  
  > The above is a complete Pipeline script. inside will:
  >
  > 1. Automatically grab an agent and a workspace (no extra node block is required).
  > 2. Pull the requested image to the Docker server (if not already cached).
  > 3. Start a container running that image.
  > 4. **Mount the Jenkins workspace as a "volume" inside the container, using the same file path.**
  
  After seeing item number four, I ran `docker container inspect <containerID>` and I saw the workspace `/home/jenkins/workspace` was actually a volume mount that was mapped to the `/home/jenkins/workspace` on the Docker host machine as well. Using this as my workspace, I was able to build the various applications within the container and then moved the results to a second volume used for application collection later in the pipeline. It's not an exact answer to my original question, but it works!
  
  
  ---
  
  ### Answer by username_2 on 2018-09-29. Score: 1
  
  If your master is running as Jenkins user, you should make a user in the docker image which you use as to create your node and do the key exchange. For example `jenkins`user with same UID needs to exist on both master and node docker container.
  
  > **Comments:**
  > **username_2**: For example jenkins user with same uid need to exist on bot master and node docker conatiner.  
  > **username_3**: Hi username_2, welcome to DevOps.SE.  You can edit your question to add more information like this.  I've submitted an edit to do this for you, but in the future you can do it.  
  
  ---
  
  