How to run Jenkins builds on Docker example

How to use Jenkins with Docker

If your Jenkins builds don’t use Docker, you’re not playing the continuous integration game on expert level.

By delegating the compile, test, package and deploy steps to a Docker container, you won’t need to supplement Jenkins installations with a suite of build tools such as Maven, Gradle or Ant.

With Jenkins and Docker integration, you won’t need to bother with maintenance and quality control software such as Sonarcube or Nexus. Just use a pre-configured Docker container and your Jenkins builds get access to all of those resources, without the headache of managing and maintaining all of that peripheral software.

The integration of Jenkins with Docker really is the smartest way to do continuous integration.

How to integrate Docker with Jenkins builds

To integrate Docker into your Jenkins builds, follow these steps

  1. Install Jenkins along with a DVCS tool such as Git
  2. Install Docker
  3. Add the Jenkins Docker plugin and Jenkins Docker pipeline plugin
  4. Give group permissions so Jenkins can run Docker images
    1. sudo usermod -a -G docker jenkins
  5. Reference Docker images in your Jenkinsfile builds

Docker in the Ubuntu terminal

The installation and configuration of Docker and Jenkins in Ubuntu requires an apt install command, a usermod command and, finally, a reboot call to ensure the usermod changes take effect:

  • sudo apt install docker.io
  • sudo usermod -a -G docker jenkins
  • reboot
Your intro to GitHub Actions training course

Here’s how to get started with GitHub Actions:

Follow these tutorials to learn GitHub Actions fast.

Docker daemon permission denied

Without the usermod command, attempts to run a Jenkins pipeline on Docker will result in the following permission error:

Permission denied while trying to connect Jenkins to the Docker daemon socket

Without the two Jenkins Docker plugins installed, any attempt to run a Jenkinsfile build will result in the following exception:

Invalid Jenkin agent type “docker” specified. Must be one of [any, label, none]

Jenkinsfile builds with Docker

The best way to demonstrate the power of Jenkins and Docker working together is to create a pipeline that builds with a Jenkinsfile.

A sample Maven project that has a Jenkinsfile in its root is hosted at the following GitHub URL:

https://github.com/learn-devops-fast/rock-paper-scissors.git
Jenkins Docker Build Example

A Jenkins Docker build pipeline need only reference a Jenkinsfile in the Git repository.

Docker Jenkins build file

The GitHub repo’s Jenkinsfile looks as follows:

pipeline {
    agent { docker { image 'maven:3.3.3' } }
      stages {
        stage('log version info') {
      steps {
        sh 'mvn --version'
        sh 'mvn clean install'
      }
    }
  }
}

When this pipeline runs, the Jenkins build happens on a Docker image. The Apache Maven calls happen not on the local OS, but inside of the container. Maven does not need to be installed on the same machine that hosts Jenkins. Only Docker is needed.

And finally, if any artifacts are created, the Jenkins Docker build places these in the workspace in the local file system so they persist after the container is taken offline. That way EAR, WAR or JAR files can be pushed to Artifactory or deployed to Tomcat for further testing or production.

To do DevOps right, you need to find the tools that best address your needs. Jenkins and Docker integration combines two of the best.