<p>I have created a <strong>Dockerfile</strong> (for a Node <strong>JNLP</strong> slave which can be used with the <strong>Kubernetes Plugin of Jenkins</strong> ). I am extending from from the official image <code>jenkinsci/jnlp-slave</code></p>

<pre><code>FROM jenkinsci/jnlp-slave

USER root


MAINTAINER Aryak Sengupta &lt;aryak.sengupta@hyland.com&gt;
LABEL Description="Image for NodeJS slave"

COPY cert.crt /usr/local/share/ca-certificates
RUN update-ca-certificates

RUN curl -sL https://deb.nodesource.com/setup_8.x | bash \
    &amp;&amp; apt-get install -y nodejs

ENTRYPOINT ["jenkins-slave"]
</code></pre>

<p>I have this image saved inside my Pod template (in K8s plugin configuration). Now, when I'm trying to run a build on this <strong>slave</strong>, I find that two containers are getting spawned up inside the Pod (A screenshot to prove the same.).</p>

<p><a href="https://i.stack.imgur.com/kkbMH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/kkbMH.png" alt="enter image description here"></a> </p>

<p>My Pod template looks like this:</p>

<p><a href="https://i.stack.imgur.com/ZqT2u.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ZqT2u.png" alt="enter image description here"></a></p>

<p>And my Kubernetes configuration looks like this:
<a href="https://i.stack.imgur.com/RHaRH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RHaRH.png" alt="enter image description here"></a></p>

<p>Now if I do a simple <code>docker ps</code>, I find that there are two containers which started up (Why?):</p>

<p><a href="https://i.stack.imgur.com/x2oYp.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/x2oYp.png" alt="enter image description here"></a></p>

<p>Now, inside the Jenkins Job configuration of Jenkins, whatever I add in the build step, the steps get executed in the first container . </p>

<p>Even if I use the official <code>Node</code> container inside my <code>PodTemplate</code>, the result is still the same:</p>

<p><a href="https://i.stack.imgur.com/w9lYP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/w9lYP.png" alt="enter image description here"></a></p>

<p>I have tried to print the Node version inside my Jenkins Job, and the output is <strong>"Node not found"</strong> . Also, to verify my haunch, I have done a <code>docker exec</code> into my second container and tried to print the <code>Node</code> version. In this case, it works absolutely fine. </p>

<p>This is what my build step looks like:</p>

<p><a href="https://i.stack.imgur.com/Chprj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Chprj.png" alt="enter image description here"></a></p>

<p>So, to boil it down, I have two major questions:</p>

<ol>
<li>Why does <strong>two</strong> separate (one for JNLP and one with all custom changes) containers start up whenever I fire up the Jenkins Job? </li>
<li>Why is my job running on the first container where Node isn't installed? How do I achieve the desired behaviour of building my project with <code>Node</code> using this configuration?</li>
</ol>

<p>What am I missing? </p>

<p>P.S. - Please do let me know if the question turns out to be unclear in some parts. </p>

<p>Edit: I understand that this can be done using the <code>Pipeline</code> Jenkins plugin where I can explicitly mention the <code>container</code> name, but I need to do this from the Jenkins UI. Is there any way to specify the container name along with the slave name which I am already doing like this:</p>

<p><a href="https://i.stack.imgur.com/XvMBn.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/XvMBn.png" alt="enter image description here"></a></p>

<p>The Jenkins kubernetes plugin will always create a JNLP slave container inside the pod that is created to perform the build. The podTemplate is where you define the other containers you need in order to perform your build.</p>

<p>In this case it seems you would want to add a Node container to your podTemplate. In your build you would then have the build happen inside the named Node container.</p>

<p>You shouldn't really care where the Pod runs. All you need to do is make sure you add a container that has the resources you need (like Node in this case). You can add as many containers as you want to a podTemplate. I have some with 10 or more containers for steps like PMD, Maven, curl, etc.</p>

<p>I use a Jenkinsfile with pipelines. </p>

<pre><code>podTemplate(cloud: 'k8s-houston', label: 'api-hire-build', 
  containers: [
    containerTemplate(name: 'maven', image: 'maven:3-jdk-8-alpine', ttyEnabled: true, command: 'cat'),
    containerTemplate(name: 'pmd', image: 'stash.company.com:8443/pmd:pmd-bin-5.5.4', alwaysPullImage: false, ttyEnabled: true, command: 'cat')
  ],
  volumes: [
    persistentVolumeClaim(claimName: 'jenkins-pv-claim', mountPath: '/mvn/.m2nrepo')
  ]
)
{
  node('api-hire-build') {
    stage('Maven compile') {
      container('maven') {
        sh "mvn -Dmaven.repo.local=/mvn/.m2nrepo/repository clean compile"
      }
    }
    stage('PMD SCA (docker)') {
      container('pmd') {
        sh 'run.sh pmd -d "$PWD"/src -f xml -reportfile "$PWD"/target/pmd.xml -failOnViolation false -rulesets java-basic,java-design,java-unusedcode -language java'
        sh 'run.sh pmd -d "$PWD"/src -f html -reportfile "$PWD"/target/pmdreport.html -failOnViolation false -rulesets java-basic,java-design,java-unusedcode -language java'
        sh 'run.sh cpd --files "$PWD"/src --minimum-tokens 100 --failOnViolation false --language java --format xml &gt; "$PWD"/target/duplicate-code.xml'
      }
      archive 'target/duplicate-code.xml'
      step([$class: 'PmdPublisher', pattern: 'target/pmd.xml'])
    }
  }
}
</code></pre>
