Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the difference between "withRun" and "inside" in Jenkinsfile?

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.

like image 216
Ismail Avatar asked Aug 13 '19 12:08

Ismail


People also ask

What is Docker image inside?

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.

What is Docker agent in Jenkins?

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.

What is Jenkinsfile container?

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.


1 Answers

Image.run([args, command])
Uses docker 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])] {…}
Like run but stops the container as soon as its body exits, so you do not need a try-finally block.

Image.inside[(args)] {…}
Like withRun 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

like image 109
Matt Schuchard Avatar answered Sep 27 '22 15:09

Matt Schuchard