This is a guest blog from Shashikant Jagtap, long time Jenkins user and leader of the London-based Jenkins Area Meetup (JAM).
Being a big fan of Jenkins, I recently came across the Jenkins 2.0 beta. The Jenkins project released beta version for users to play around with the new features of Jenkins 2.0. The new version of Jenkins, a.k.a Jenkins 2.0, is available now! Jenkins 2.0 is bringing us some new amazing features:
Improved user interface and user experience
Security and plugin enhancements
New Jenkins website as a one-stop shop for a Getting Started Guide and other documentation
The detailed information about the new release can be found here.
In this post, let’s try out the new version of Jenkins with its own Docker image.
Jenkins Inside Docker
Assuming that, you have a Docker ToolBox or similar setup on your machine, you can get the latest image. You can follow my previous post on practical guide to install Docker ToolBox. You can get the image just by pulling it from Docker Hub.
$ docker pull jenkinsci/jenkins:2.0-beta-2
I have found some cool write-ups on RiotGames blogs about setting up Docker and Jenkins. It’s worth having a look in order to gain a detailed understanding of Docker and Jenkins.
Jenkins Master and Mounted Data Volume
Let’s build a couple of Docker images in order to setup a Jenkins instance with preserved data. There will be two images and containers:
To run Jenkins Master
Stored Data so that restarting the container won’t lose jobs and plugin data
Let’s create a Dockerfile to build our Jenkins master:
$ mkdir jenkins2-docker $ cd jenkins2-docker $ vim Dockerfile
Now insert the following in the Dockerfile:
FROM jenkinsci/jenkins:2.0-beta-1 USER root RUN mkdir /var/log/jenkins RUN mkdir /var/cache/jenkins RUN chown -R jenkins:jenkins /var/log/jenkins RUN chown -R jenkins:jenkins /var/cache/jenkins USER jenkins ENV JAVA_OPTS="-Xmx8192m"
In this Dockerfile, we are creating another image on top of the Jenkins Docker image with some Jenkins log directory and Java options.
Now, we will create another Dockerfile called ‘Dockerfile-data’ so that we can mount the image/containers while launching jenkins-master:
$ vim Dockerfile-data
Add the following content to the Dockerfile:
FROM debian:jessie # Create the jenkins user RUN useradd -d "/var/jenkins_home" -u 1000 -m -s /bin/bash jenkins # Create the folders and volume mount points RUN mkdir -p /var/log/jenkins RUN chown -R jenkins:jenkins /var/log/jenkins VOLUME ["/var/log/jenkins", "/var/jenkins_home"] USER jenkins CMD ["echo", "Data container for Jenkins"]
This image will be used to mount a volume for the Jenkins master so that we can delete the Jenkins master without worrying about the data.
Build Docker Images and Run Containers
Now that we have a Dockerfile for the Jenkins master and Jenkins data, we can build those images:
$ docker build -t jenkins-data -f Dockerfile-data .
$ docker build -t jenkins2 .
We can launch the containers for each image:
$ docker run --name=jenkins-data jenkins-data $ docker run -p 8080:8080 -p 50000:50000 --name=jenkins-master --volumes-from=jenkins-data -d jenkins2
Now we can see Jenkins 2.0 will be started on Docker host IP and Port 8080 e.g.: http://192.168.99.100:8080/
Setting Up Jenkins 2.o
Now that we are running an instance of Jenkins at ‘http://192.168.99.100:8080/’ (Assuming 192.168.99.100 is your Docker-Machine or equivalent IP), we can setup up some additional things:
Jenkins 2.0 will ask for the admin password stored in the Jenkins master container. We can get it and paste it in the console:
$ docker exec jenkins-master cat /var/jenkins_home/secrets/initialAdminPassword
The next step is to fill in the required user details in order to login to Jenkins. You can then install the default plugins.
Now we have a ready instance of Jenkins to configure Jobs and Pipelines:
Exploring Jenkins 2.0 Features
Jenkins features are explained in the brief above and we will demonstrate two powerful features:
This was available in the previous version of Jenkins, but it’s now easier than ever before. Pipeline-as-code has the following benefits:
Easily define simple and complex pipelines through the DSL in a Jenkinsfile.
Pipeline-as-code provides a common language to help teams (e.g. Dev and Ops) work together.
Easily share pipelines between teams by storing common “steps” in shared repositories.
Here is the demo of how to create a default pipeline using Jenkins 2.0. — see below:
Now we can create different pipelines as per the GitHub branches. I have created a Github repo ‘jenkins2-docker’ with multiple branches and configured Jenkins accordingly.