Get Started with Jenkins 2.0 with Docker

Written by: Hannah Inman

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:

  • Pipeline-as-code
  • 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 it's own Docker image .

Jenkins Inside Docker

The fastest way to get started is to get the Docker image and run containers. The Jenkins project has already created a Docker image jenkinsci/jenkins with tag of '2.0-beta-2'.

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:

  • Two manage Jenkins Master
  • Store Data so that restarting 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:

  • Jenkins data image

$ docker build -t jenkins-data -f Dockerfile-data . 
  • Jenkins master image

$ 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/

dj.gif

Setting Up Jenkins 2.0

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:

  • Admin Password

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 
  • User Details

The next step is to fill in the required user details in order to login to Jenkins.

  • Plugins

You can then install the default plugins.

Now we have a ready instance of Jenkins to configure Jobs and Pipelines.

jenkins-startup.gif

Exploring Jenkins 2.0 Features

Jenkins features are explained in the brief above and we will demonstrate two powerful features:

Pipeline-as-code

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:

pipeline.gif

Multi-branch Pipeline

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.

multibranch_pipeline.gif

There are some other features of Jenkins 2.0 that will be released and I am very excited about it.

Note : Source code for this demo is available on GitHub Repo called ‘Jenkins2-Docker’.

Hope you enjoy Jenkins 2.0!
Shashikant Jagtap

shashi.JPG

Shashikant is a DevOps and automation engineer with massive interest in BDD, DevOps, CI and CD tools. He is a big fan of tools like Cucumber, Docker, Jenkins, Appium, Ansible and loves Linux. Shashikant has been a Jenkins user for a long time and is the current leader of the London Jenkins Area Meetup (JAM) .

He blogs about BDD and cutting edge open source tools. You can read more blog posts from Shashikant on his b log website: http://shashikantjagtap.net

 

 

 


Learn More

Watch this webinar to learn about what’s new in Jenkins 2 for users, configuring traditional jobs, orchestrating delivery pipelines and where Jenkins is going in the future.

 

 


 

Stay up-to-date with the latest insights

Sign up today for the CloudBees newsletter and get our latest and greatest how-to’s and developer insights, product updates and company news!