Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easiest way to do docker build command within Jenkinsfile running on Jenkins slave node?

Basic example of what I want my Jenkinsfile to do:

node {
   sh 'docker build -t foo/bar .'
}

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)

Are my assumptions correct?

  1. When running with Jenkins master/slaves, the Jenkinsfile is executed by a Jenkins slave
  2. 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?

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

Currently it errors out saying "docker: not found"

like image 212
gunit Avatar asked Aug 03 '17 20:08

gunit


People also ask

Can we use Docker container as a node in Jenkinsfile?

Many organizations use Docker to unify their build and test environments across machines, and to provide an efficient mechanism for deploying applications. Starting with Pipeline versions 2.5 and higher, Pipeline has built-in support for interacting with Docker from within a Jenkinsfile .

How do I make Docker images automatically with Jenkins pipeline?

Setting up your environmentInstall the Docker Pipelines plugin on Jenkins: Manage Jenkins → Manage Plugins. Search Docker Pipelines, click on Install without restart and wait until is done. Upload your Dockerfile definition to your Github repository.


1 Answers

My assumption is that you want to docker build inside the Jenkins slave (which is a Kubernetes pod, I assume created by the Kubernetes Jenkins Plugin)

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).

So you are actually trying to run a Docker command inside a container based on gcr.io/cloud-solutions-images/jenkins-k8s-slave, which is most likely based on the official Jenkins JNLP Slave, which does not container Docker!

From this point forward, there are two approaches that you can take:

  • use a slightly modified image based on the JNLP slave that also contains the Docker client and mount the Docker socket (/var/run/docker.sock) inside the container. (You can find details on this approach here). Here is an image that contains the Docker client and kubectl.

Here is a complete view of how to configure the Jenkins Plugin:

enter image description here

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.

  • 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 use the JNLP slave to start other containers in the same pod.

Here is the full file from the image below

Here is the Jenkins Plugin documentation for doing this

enter image description here

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.

These are the two ways I found to achieve building images inside a Jenkins JNLP slave running inside a container.

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.

Some more resources:

  • deploy Jenkins to Kubernetes as Helm chart, configure plugins to install

Thanks, Radu M

like image 142
radu-matei Avatar answered Sep 23 '22 22:09

radu-matei