Jenkins Docker Pipeline override working directory

jenkins pipeline dir
docker version is older than 17.12 working directory will be
jenkins pipeline check if directory exists
jenkins copy files to docker
jenkins pipeline docker mount volume
jenkins docker withrun
running docker in jenkins
docker plugin jenkins

I have noticed when using Jenkins with the Docker Pipeline plugin. When I create a Jenkins file to run commands in a docker container that it always creates a volume mapping of the Jenkins workspace directory mapped to the same path in the running container. It also creates a working directory with the same path.

docker run -t -d -u 127:134 -w /var/lib/jenkins/workspace/DockerTest 
-v /var/lib/jenkins/workspace/DockerTest:/var/lib/jenkins/workspace/DockerTest:rw,z 
-v /var/lib/jenkins/workspace/DockerTest@tmp:/var/lib/jenkins/workspace/DockerTest@tmp:rw,z 
-e ******** 
-e ******** 
-e ******** 
-e ******** 
-e ******** 
-e ******** 
-e ******** 
-e ******** 
-e ******** 
-e ******** 
-e ******** 
-e ******** 
-e ******** 
-e ******** 
-e ******** 
-e ******** 
-e ******** 
-e ******** 
jekyll/jekyll:builder cat

I tried to override this by providing arguments in my Jenkinsfile for Docker like this:

pipeline {
  agent {
    docker {
      image 'jekyll/jekyll:builder'
      args '-v $HOME:/srv/jekyll -w /srv/jekyll'
    }
  }
  stages {
    stage('Test') {
      steps {
      sh 'cd /srv/jekyll && ls -l'
      }
    }
  }
}

It seems that this just prepends the options to the Docker command and the workdir and volume mapping gets overwritten by the default settings:

docker run -t -d -u 127:134 
-v $HOME:/srv/jekyll 
-w /srv/jekyll 
-w /var/lib/jenkins/workspace/DockerTest 
-v /var/lib/jenkins/workspace/DockerTest:/var/lib/jenkins/workspace/DockerTest:rw,z 
-v /var/lib/jenkins/workspace/DockerTest@tmp:/var/lib/jenkins/workspace/DockerTest@tmp:rw,z 
-e ******** 
-e ******** 
-e ******** 
-e ******** 
-e ******** 
-e ******** 
-e ******** 
-e ******** 
-e ******** 
-e ******** 
-e ******** 
-e ******** 
-e ******** 
-e ******** 
-e ******** 
-e ******** 
-e ******** 
-e ******** 
jekyll/jekyll:builder cat

Is there any way that I can override the volume mappings and working directory in any way?


@Nagri unfortunately I could still not specify the working directory and volume mapping.

But I could run jekylll as root.

It is not ideal but at least it works.

```

pipeline {
    agent {
        docker {
            image 'jekyll/jekyll:builder'
            args '-u root'
        }
    }
    stages {
        stage('Test') {
            steps {
                sh 'mkdir -p _site'
                sh 'jekyll build'
            }
        }
    }
}

```

[JENKINS-35406] Image.inside overrides working directory, @Nagri unfortunately I could still not specify the working directory and volume mapping. But I could run jekylll as root. It is not ideal but at least it works. ``` Powered by a free Atlassian Jira open source license for Jenkins. Try Jira - bug tracking software for your team.


You can combine docker {} with dir {} to control the working directory:

pipeline {
  agent {
    docker {
      image 'jekyll/jekyll:builder'
      args '-v $HOME:/srv/jekyll'
    }
  }
  stages {
    stage('Test') {
      steps {
        dir(path: '/srv/jekyll') {
          sh 'ls -l'
        }
      }
    }
  }
}

I've dropped the -w argument, since it has no effect, and replaced cd /src/jekyll with dir(path: '/srv/jekyll'){...}

Unable to pass workdir · Issue #561 · jenkinsci/docker-plugin · GitHub, Priority: Minor. Resolution: Duplicate. Component/s: docker-workflow-plugin. Labels: pipeline. Environment: Jenkins 2.6. Similar Issues: I have a jenkins master on linux, a slave agent on windows 10. On the slave there is docker desktop installed. I can run commands under this docker from windows command line, for example:


we had a same requirement to use work directory different than the default one in container, after checking it understood as of now its not possible. check here for more details JENKINS-41474, JENKINS-35406, JENKINS-33510

the other way is to use dir("path_to_work") { sh ' '}, but it did not worked either. so only way is to mount the volume in container with args and run all the commands with change directory command in it

  pipeline {
  agent {
    docker {
      image 'alpine:3.7'
      args '-v $HOME/src:/src'
    }
  }
  stages {
    stage('Build') {
      steps {
      sh 'cd /src ; ./run_build.sh'
      }
    }
  }
}

Docker agent sets wrong workdir · Issue #663 · jenkinsci/docker , latter -w wins, override the -w my-workdir in inside() block, All process ran by Jenkins should run with current workspace as working dir, don't they ? If you're not controlling your docker stuff from pipelines but are, instead,  Using Docker in Pipeline can be an effective way to run a service on which the build, or a set of tests, may rely. Similar to the sidecar pattern, Docker Pipeline can run one container "in the background", while performing work in another. Utilizing this sidecar approach, a Pipeline can have a "clean" container provisioned for each Pipeline run.


I had a similar issue once, the problem was not the working directory, as I was using ENV variables to access my binaries from anywhere.

My issue was a permissions issue, even though I did not see any error message regarding permissions. Simply execute the command as the root user and see if that works , e.g. :

pipeline {
  agent {
    docker {
      image 'jekyll/jekyll:builder'
      args '-v $HOME:/srv/jekyll -w /srv/jekyll -u root'
    }
  }
  stages {
    stage('Test') {
      steps {
      sh 'cd /srv/jekyll && ls -l'
      }
    }
  }
}

Using Docker with Pipeline, I have this pipeline snippet: stage('Build') { agent { docker { image 'node:8' args '-v ${WORKSPACE}/frontend://app -w /app Docker Pipeline Plugin Version is 1.17 on Jenkins ver. I also tried something like docker.image(xxxx).inside('-w workdir') to override the former workdir, but it doesn't seem to work. deleteDir: Recursively delete the current directory from the workspace. Recursively deletes the current directory and its contents. Symbolic links and junctions will not be followed but will be removed. To delete a specific directory of a workspace wrap the deleteDir step in a dir step.


Pipeline: Basic Steps, Pipeline is designed to easily use Docker images as the execution environment for a single The build() method builds the Dockerfile in the current directory by default. This example overrides the default Dockerfile by passing the -f flag:. Dismiss Join GitHub today. GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.


dockerized jenkins failed building dockerfile work dir issue, Jenkins Use-cases - Android - Bitbucket Server - C/C++ - Docker - Embedded If the shell step fails, the Pipeline build's status will be set to failed, so that the To delete a specific directory of a workspace wrap the deleteDir step in a dir step for the env singleton for more information on environment variables. overrides. When I try to override the jnlp container image I still always get the alpine image and do not get the specified image. It seems that event hough i specify a container with the name jnlp it just ignores it and uses the default alpine image.


Jenkinsfiles for Beginners and Masochists, I've solved my issue. I had to change the way I call my Dockerfile and changing the WORKDIR inside. What it does is simply create a new folder called "new-dir" in the current working directory and then the pwd prints out the current working directory instead of navigating into the "new-dir". Am I misusing this step because I can't find many examples online.