Jenkins is a continuous integration and continuous delivery server that allows developers to build and test their software rapidly. Jenkins also comes with a host of powerful plugins that allows for a devops to build powerful continuous delivery pipeline. In its purest form, Jenkins already comes bundled as a war file and can simply be run by issuing a command
java -jar jenkins.war
However integrating Jenkins with docker will give you several advantages. You can build ready to run containers right from Jenkins without an intermediate step thus automating the step to build your prod runtime environment. You can run your tests in brand new environments every time, each with their own unique dependencies, versions etc. thanks to docker. However before you can integrate Jenkins with docker, you can run Jenkins from within docker itself.
[youtube https://www.youtube.com/watch?v=W5p0sd5UHrQ]
- Running Jenkins – The simplest way to get Jenkins up and running is to simply run the Jenkins container. Run the command below.
docker run -p 8080:8080 -p 50000:50000 jenkins
This will download and run the official Jenkins container and map the internal 8080 port to the 8080 port on your host. Port 50000 is also mapped to your host in case you want to run any slaves and bind to this server (not covered in this post). Now connect to port 8080 on your docker machine and your should see a Jenkins server running.
- Saving the Configuration – As soon as you shutdown the container,docker will destroy anything inside the container including any configuration changes you made. If you want to make your configurations permanent, you will have to mount a volume inside the container (Read up on docker volumes). To create and mount a volume as Jenkins home, run the command
docker run --name jenkins-server -p 8080:8080 -p 50000:50000 -v /var/jenkins_home jenkins
The configuration will now be persisted at a volume at /var/jenkins_home. Please note the video incorrectly states that the volume is called /var/jenkins_home. This is incorrect. As you can see by running docker inspect the real name of the volume is rather long and it is simply mounted at /var/jenkins_home
- Add more build executors – By default Jenkins has two build executors. However sometimes it pays to have more build executors working for you, specially if you have a server with some power. To increase the number of build executors, create a groovy file called executors.groovy with the contents as shown
import jenkins.model.* Jenkins.instance.SetNumExecutors(7)
Save this file in a directory. In the same directory create a Dockerfile with the contents below.
FROM jenkins COPY executors.groovy /usr/share/jenkins/ref/init.groovy.d/executors.groovy
Now run the command
docker build -t /jenkins-server .
Your docker username is optional and you can only use it if you want to upload the fruits of your labor to docker hub. Once the container builds, run it with the command
docker run -p 8080:8080 -p 50000:50000 /jenkins-server .
You should now see 7 active executors.
- Installing Plugins – What is a Jenkins server without some plugins ? Well it’s a plugin less Jenkins server, but you may still want to install some plugins. Why not install the docker plugin itself. This plugin will allow you to run builds inside of docker containers (not covered here). In the same directory as the Dockerfile create a file called plugins.txt and add the contents as below
token-macro:1.12.1 durable-task:1.3 docker-plugin:0.16.0
In general the plugins file has key value pairs with the plugin name as the key and the revision number of the plugin as the value. Now edit your Dockerfile file so it looks like this
FROM jenkins COPY executors.groovy /usr/share/jenkins/ref/init.groovy.d/executors.groovy COPY plugins.txt /usr/share/jenkins/plugins.txt RUN /usr/local/bin/plugins.sh /usr/share/jenkins/plugins.txt
And that’s it. Build the container with the commands in step 3. And then run it with the commands in step 3. You will now see the docker plugin installed and available for use.
All the code used in this article is also available here https://github.com/nimkar/jenkins-server.
Leave a comment below with your setup and experience or if you have any questions.
. I just read through your article ,it was awesome i planing to follow your blog in future also.thanks for sharing please share more article like this. and thanks for sharing the video .really helpful .