Docker – Using Jenkins to build Docker images

How to enable Remote Desktop on Windows Server 2019

You probably already know that Jenkins is a widely-used tool for continuous integration/continuous delivery (CI/CD) systems. Virtually every company, both large and small, is using it in some capacity. It is extremely effective, and highly configurable, especially with the variety of plugins that can be used with it. So, it is very natural to expand its use to create Docker images. This first step in using Jenkins with Docker is pretty easy to accomplish. If you have an existing Jenkins server in use today, all you need to do to use it to build Docker images is to install Docker on the Jenkins server. You use the exact same installation techniques that we saw and used in Chapter 1, Setting up a Docker Development Environment. Based on the OS of the system that is running your Jenkins server, you follow the install steps you learned in Chapter 1, Setting up a Docker Development Environment; when you are done, you can use Jenkins to build Docker images.

If you don’t already have a Jenkins server up and running, you can follow the guide found in the Installing Jenkins web page link in the following References section and install Jenkins on whatever OS you’re using. As an example, we will be using the information from that page to set up a Jenkins server on an Ubuntu system. Start by opening a terminal window. Now get the apt-key for Jenkins packages. Next, you will add the Debian Jenkins source to the apt sources list. Next, you will update the packages on the system, and finally, you will install Jenkins using apt-get. The commands look like the following:

# If Java has not yet been installed, install it now
sudo apt install openjdk-8-jre-headless

# Install Jenkins on an Ubuntu system
wget -q -O - | sudo apt-key add -
sudo sh -c 'echo deb binary/ > /etc/apt/sources.list.d/jenkins.list'
sudo apt-get update
sudo apt-get install jenkins

Running these commands on my system looks like the following:

When the install completes, you will want to open your browser and browse to port 8080 on the system to finish the setup and configuration of your Jenkins system. This will include entering the admin password and then deciding which plugins to install as part of the initial deployment of your Jenkins server. I recommend using the set recommended by Jenkins as it is a great starting point:

Now that you have a Jenkins server, you can begin creating jobs for it to execute to confirm that it is working as desired. Let’s start out with a trivial Hello world! job to confirm that Jenkins is working. Log into your Jenkins server and click on the New Item link. In the new item page, enter the name for our job. I’m using hello-test. Select the type of job that we want to create as pipeline. Next, click the OK button near the bottom left of the page. This will take you to the configuration screen for our new job. This one is going to be very simple. We are going to create a pipeline script, so scroll down until you see the Pipeline script input box, and enter the following script (note that the pipeline script is written in groovy, which uses the Java (and C) form of comments):

// Our hello world pipeline script, named "hello-test"
node {
  stage('Say Hello') {
      echo 'Hello Docker Quick Start Guide Readers!'

That’s all for now, so click on the Save button to save the updated configuration of our Jenkins job. Once the configuration has been saved, let’s test the job by clicking on the Build now link. If everything is functioning as expected, we should see the job complete successfully. It will look something like the following:

Now let’s create another job. Click the link to go back to the dashboard and then click the New Item link again. This time, let’s name the job hello-docker-test. Again, select the pipeline for the type of job you want to create and then click the OK button. Again, scroll down to the Pipeline script input and enter the following:

// Our Docker hello world pipeline script, named "hello-docker-test"
node {
   stage('Hello via Alpine') {
      docker.image('alpine:latest').inside {
         sh 'echo Hello DQS Readers - from inside an alpine container!'

Click on the Save button to save the configuration for the new job, and then click the Build Now link to launch the Jenkins job. The following is what it might look like this time:

What happened this time? This one didn’t complete successfully. Well, obviously it failed because we don’t have Docker installed on our Jenkins server yet. So let’s go ahead and follow the instructions found in Chapter 1, Setting up a Docker Development Environment, for installing Docker, and install it on our Jenkins server. Once you have it installed, there is one additional step you will want to do, which is to add the Jenkins user to the Docker group. The following is the command:

# Add the jenkins user to the docker group
sudo usermod -aG docker jenkins
# Then restart the jenkins service
sudo service jenkins restart

It is very much like the command we used to add the current user of our Docker server to the docker group so that it was unnecessary to use sudo for Docker commands. OK, now let’s go back to our Jenkins server UI and to our hello-docker-test job and click the Build now button again.

Congratulations! You have a shiny, new Jenkins server, properly configured to build (test, push, and deploy) Docker images. Well done. Still, while this is a great accomplishment, it was kind of a lot of work. Don’t you wish there was an easier way to set up a new Jenkins server? So, you know how you already have a nice set of servers running Docker? Do you think you can use that environment to stand up your Jenkins server in an easier way? You betcha! Let’s take a look.

Comments are closed.