How to run Jenkins from Docker

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

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.

  1. Running Jenkins – The simplest way to get Jenkins up and running is to simply run the Jenkins container. Run the command below.

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

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

    Save this file in a directory. In the same directory create a Dockerfile with the contents below.

    Now run the command

    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

    You should now see 7 active executors.
  4. 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

    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

    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

Leave a comment below with your setup and experience or if you have any questions.

1 Comment

Leave a Reply

Your email address will not be published.