HiveBrain v1.2.0
Get Started
← Back to all entries
patterndockerMinor

Is there an alternative to the Jenkins dir() to change to a directory on a Docker container?

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
directorythealternativecontainerjenkinsdockerdirtherechange

Problem

Here's my current set-up:

  • Jenkins Master running in Docker on an Ubuntu 18.04 host.



  • A Node running Ubuntu 18.04 with Docker installed.



  • 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.(FileMonitoringTask.java:171)
at org.jenkinsci.plugins.durabletask.BourneShellScript$ShellController.(BourneShellScript.java:197)
at org.jenkinsci.plugins.durabletask.BourneShellScript$ShellController.(BourneShellScript.java:189)
at org.jenkinsci.plugins.durabletask.BourneShellScript.launchWithCooki

Solution

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.

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:

  • Automatically grab an agent and a workspace (no extra node block is required).



  • Pull the requested image to the Docker server (if not already cached).



  • Start a container running that image.



  • 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 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!

Context

StackExchange DevOps Q#5084, answer score: 2

Revisions (0)

No revisions yet.