Docker – Setting up a Dockerized Jenkins server

How to Create a Droplet with DigitalOcean

You have just seen how much work it is to set up a new Jenkins server. While it is not a Herculean effort, there are at least five steps you have to do before you can pick your plugins and log in to get to work. And in the spirit of the game show Name That Tune, I can deploy a Jenkins server in three steps, and the first two are just to allow our Jenkins data to persist beyond the life of the Docker container that hosts the Jenkins server. Assuming you have a Docker host set up-and-running as per the instructions in Chapter 1, Setting up a Docker Development Environment, we want to create a location for the Jenkins server to store its data. We will create a folder and assign ownership to it. It will look like the following:

# Setup volume location to store Jenkins configuration
mkdir $HOME/jenkins_home
chown 1000 $HOME/jenkins_home

The owner 1000 is the user ID that will be used for the jenkins user inside the Docker container.

The third step is to deploy our container. Before I show you the command, let me talk a little about which container image to use. I am including a link for searching on the Docker hub for Jenkins images. If you use that link or search on your own, you will see that there are a lot of options to choose from. Initially, you might think about using the official Jenkins image. However, if you browse to that repo, you will find what I feel is kind of odd, which is that the official image is deprecated. It has stopped being updated past version LTS 2.60.x:

It recommends using the image found in the  jenkins/jenkins:lts Jenkins repo, which at the time of writing is version 2.149.x. This is the image we will use in the following example. The following is the command we are going to use to deploy our Jenkins server container:

# Deploy a Jenkins server that is configured to build Docker images
docker container run -d -p 8080:8080 -p 50000:50000 \
-v $HOME/jenkins_home:/var/jenkins_home \
--name jenkins --rm jenkins/jenkins:lts

Taking a closer look at this command, we see that we are launching the container as a daemon (non-interactively). We see that we are opening two ports on the host, which are mapped to the same port numbers on the container, specifically 8080 and 50000. Next, we see that we are using a volume, and it is mapping to the folder we created earlier. This is where Jenkins will store its data, such as the jobs we create and the status of their execution. Then you will notice we are nameing the container jenkins. After that, we tell Docker to remove the container when it exits using the --rm flag. Finally, we tell Docker what image we want to run. 

When you run this container, giving it a minute or two to start up and browse to port 8080 on the Docker host, you will see the same prompt for a password that you see when you deploy Jenkins as a standalone application. That will be followed by the create-the-first-user screen and the default-plugin-configuration screen. Go ahead and give it a try.

Since we have created a volume for the Jenkins data (written to /var/jenkins_home), our Jenkins configuration data is being saved to the host and will live beyond the life of the container itself. Of course, you can use a storage driver and have this data somewhere more permanent than the Docker host, but you get the idea, right? 

The only problem is that neither the official Jenkins image nor the jenkins/jenkins image supports creating jobs that will build a Docker image. And since this book is all about Docker, we need to do something more than just run our Jenkins server using the aforementioned images. Don’t worry, I have a plan for that… Keep reading.

Comments are closed.