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?
To change the Jenkins Home directory on Linux, create a new Home directory, copy the contents of the old Home directory to the new one and edit the Jenkins configuration file. In the example below, we are using Ubuntu 18.04.
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'){...}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With