<p>Basic example of what I want my Jenkinsfile to do:</p>

<pre><code>node {
   sh 'docker build -t foo/bar .'
}
</code></pre>

<p>It seems like I need to install docker onto the Jenkins slave image that's executing my Jenkinsfile. Is there an easy way of doing this? (That Jenkins slave image is itself a docker container)</p>

<p>Are my assumptions correct?</p>

<ol>
<li>When running with Jenkins master/slaves, the Jenkinsfile is executed by a Jenkins slave</li>
<li>Jenkins plugins installed via the Manage Plugins section (e.g. the Docker Plugin, or Gcloud SDK plugin) are only installed on the Jenkins masters, therefore I would need to manually build my Jenkins slave docker image and install docker on the image?</li>
</ol>

<p>Since I also need access to the 'gcloud' command (I'm running Jenkins via Kubernetes Helm/Charts), I've been using the <code>gcr.io/cloud-solutions-images/jenkins-k8s-slave</code> image for my Jenkins slave.</p>

<p>Currently it errors out saying "docker: not found"</p>

<p>My assumption is that you want to <code>docker build</code> inside the Jenkins slave (which is a Kubernetes pod, I assume created by the <a href="https://github.com/jenkinsci/kubernetes-plugin" rel="noreferrer">Kubernetes Jenkins Plugin</a>)</p>
<p>To set the stage, when Kubernetes creates pod that will act as a Jenkins slave, all commands that you execute inside the node will be executed inside that Kubernetes pod, inside one of the containers there (by default there will only be one container, but more on this later).</p>
<p>So you are actually trying to run a Docker command inside a container based on <code>gcr.io/cloud-solutions-images/jenkins-k8s-slave</code>, which is most likely based on <a href="https://github.com/jenkinsci/docker-jnlp-slave" rel="noreferrer">the official Jenkins JNLP Slave</a>, <strong>which does not container Docker!</strong></p>
<p>From this point forward, there are two approaches that you can take:</p>
<ul>
<li>use a slightly modified image based on the JNLP slave that also contains the Docker client and mount the Docker socket (<code>/var/run/docker.sock</code>) inside the container.
(<a href="https://radu-matei.com/blog/kubernetes-jenkins-azure/#the-docker-image-for-the-slaves" rel="noreferrer">You can find details on this approach here</a>).
<a href="https://github.com/radu-matei/jenkins-slave-docker" rel="noreferrer">Here is an image that contains the Docker client and <code>kubectl</code></a>.</li>
</ul>
<p>Here is a complete view of how to configure the Jenkins Plugin:</p>
<p><a href="https://i.stack.imgur.com/rCeKT.png" rel="noreferrer"><img src="https://i.stack.imgur.com/rCeKT.png" alt="enter image description here" /></a></p>
<blockquote>
<p>Note that you use a different image (you can create your own and add any binary you want there) and that you mount the Docker socket inside the container.</p>
</blockquote>
<ul>
<li>the problem with the first approach is that you create a new image forked from the official JNLP slave and manually add the Docker client. This means that whenever Jenkins or Docker have updates, you need to manually update your image and entire configuration, which is not that desirable.
Using the second approach you always use official images, and you <strong>use the JNLP slave to start other containers in the same pod.</strong></li>
</ul>
<blockquote>
<p><a href="https://github.com/radu-matei/kube-bot/blob/master/Jenkinsfile" rel="noreferrer">Here is the full file from the image below</a></p>
<p><a href="https://github.com/jenkinsci/kubernetes-plugin#container-group-support" rel="noreferrer">Here is the Jenkins Plugin documentation for doing this</a></p>
</blockquote>
<p><a href="https://i.stack.imgur.com/qt53n.png" rel="noreferrer"><img src="https://i.stack.imgur.com/qt53n.png" alt="enter image description here" /></a></p>
<p>As I said, the JNLP image will start a container that you specify in the same pod. Note that in order to use Docker from a container you still need to mount the Docker sock.</p>
<p>These are the two ways I found to achieve building images inside a Jenkins JNLP slave running inside a container.</p>
<p>The example also shows how to push the image using credential bindings from Jenkins, and how to update a Kubernetes deployment as part of the build process.</p>
<p>Some more resources:</p>
<ul>
<li><a href="https://github.com/kubernetes/charts/tree/master/stable/jenkins" rel="noreferrer">deploy Jenkins to Kubernetes as Helm chart, configure plugins to install</a></li>
</ul>
<p>Thanks,
Radu M</p>
