Run Jenkins on Docker
Our Journey to Continuous Delivery: Chapter 2 of 4
In the first article of this series, we explained our growing pain with our CI/CD. We arrived at a point where we decided to use Docker for our build system.
This article will get you started with Docker, more precisely, will get you a Jenkins server running using Docker.
What is Docker?
Docker containers wrap a piece of software in a complete filesystem that contains everything needed to run: code, runtime, system tools, system libraries — anything that can be installed on a server. This guarantees that the software will always run the same, regardless of its environment. (source docker.com)
What is covered in this article?
Our build system uses a container for every component, the CI application itself and the builds. This article focuses on the Jenkins application container as the build containers will be covered separately. In Jenkins terminology, the Jenkins application is also called Jenkins Master, while the builds are executed on Jenkins Slaves.
Get a running Jenkins within minutes
To best follow along, a working example can be run once you have Docker installed. Simply execute the command below in your terminal:
docker run --name jenkins -p 8080:8080 -v /var/jenkins_home ticketfly/jenkins-example-gradle-build
When running this single command, it executes multiple different steps:
- Download the image called ticketfly/jenkins-example-gradle-build from dockerhub.
- Create a Docker volume inside the container at /var/jenkins_home (the Jenkins home directory)
- Run Jenkins on port 8080 (as set by the parameter -p)
- The Jenkins image comes with a build called an-example-of-github-project. The file is stored as an XML file in the image.
- When starting Jenkins, the build is executed. It clones a github repo and builds the project.
The provided example is an illustration of how fast and powerful Docker is for configuring Jenkins.
If you would have done everything manually you would have to:
- Install Java
- Intall Jenkins
- Install required plugins
- Configure Jenkins
- Create a new build
- Run the build
If you have already worked with Jenkins, it would have taken you an hour or so on a new machine. Up to a few hours if you have never done it.
Build the image from the source code and make some local changes
To get the example running locally, run the following 3 commands:
#1 git clone https://github.com/Ticketfly/jenkins-docker-examples
#2 cd jenkins-docker-examples/01-gradle-build
#3 ./gradlew dockerBuild dockerRun[...]
:dockerBuild
[...]
Successfully built 64d5555fa970
:dockerRun
[...]
INFO: Jenkins is fully up and running
#1 downloads the github repo
#2 select the docker image called 01-gradle-build
#3 run the docker commands using gradle.
- dockerBuild: we provided a custom gradle task calling the docker command build with the default parameters for this project.
- dockerRun: we provided a custom gradle task calling the command docker run with the default parameters for this project.
The output is the same as when running previously, but this time instead of using the image from dockerhub, the image was built locally and the local version is executed.
At this point, with minimum knowledge of Jenkins you should be able to make the following changes:
- Change the version of Jenkins in the Dockerfile.
- Update the Jenkins plugins in plugins.txt
- Run some groovy code such as run a build in the startup script.
- Provide some Jenkins job config files to be deployed in the jobs folder. You can manually create the jobs and capture their config.xml.
After making the changes rerun ./gradlew dockerBuild dockerRun to rebuild the image and test it.
This article demonstrates how you can quickly get a Jenkins server running using docker. With little knowledge of Docker you should be able to make some changes to the image and test locally by running your version of the Docker image. Feel free to comment if you have questions or get stuck.
If you have a lot of projects to configure, we would recommend using the Jenkins DSL, this will be covered in our next chapter.