I have a Jenkinsfile in which I am trying to execute npm run test
inside a container.
When I run with inside
it fails but when I run with withRun
it runs as I want to.
Code for reference
with inside
stage('Test') {
docker.image('justinribeiro/chrome-headless').inside ("-p 9222:9222 --security-opt seccomp=$WORKSPACE/chrome.json") {
sh label:
'Running npm test',
script: '''
npm run test
'''
}
}
With withRun
stage('Test') {
docker.image('justinribeiro/chrome-headless').withRun ("-p 9222:9222 --security-opt seccomp=$WORKSPACE/chrome.json") {
sh label:
'Running npm test',
script: '''
npm run test
'''
}
}
Now I want to understand what is the difference between them.
I have observed that inside
adds volumes and runs cat
on container whereas withRun
does not.
I also read the documentation https://jenkins.io/doc/book/pipeline/docker/ but did not understand well enough.
A much more detailed explanation will be much appreciated.
Thanks.
A Docker image contains application code, libraries, tools, dependencies and other files needed to make an application run. When a user runs an image, it can become one or many instances of a container. Docker images have multiple layers, each one originates from the previous layer but is different from it.
It is a Jenkins Cloud plugin for Docker. The aim of this docker plugin is to be able to use a Docker host to dynamically provision a docker container as a Jenkins agent node, let that run a single build, then tear-down that node, without the build process (or Jenkins job definition) requiring any awareness of docker.
The Jenkinsfile produces a pod with two containers, as specified. The two containers share a working directory and a volume mount that defaults to /home/jenkins/agent . The jnlp container takes care of the declarative checkout source code management (SCM) action.
Image.run([args, command])
Usesdocker run
to run the image, and returns a Container which you could stop later. Additional args may be added, such as '-p 8080:8080 --memory-swap=-1'. Optional command is equivalent to Docker command specified after the image. Records a run fingerprint in the build.
Image.withRun[(args[, command])] {…}
Likerun
but stops the container as soon as its body exits, so you do not need atry-finally
block.
Image.inside[(args)] {…}
LikewithRun
this starts a container for the duration of the body, but all external commands (sh) launched by the body run inside the container rather than on the host. These commands run in the same working directory (normally a Jenkins agent workspace), which means that the Docker server must be on localhost.
So as you can tell from the above, your sh
method commands (specifically the npm
commands) will execute on the host for withRun
, but within the container for inside
.
Link to the docs: https://opensource.triology.de/jenkins/pipeline-syntax/globals
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