Get started Bring yourself up to speed with our introductory content.

How to run Jenkins builds on Docker

Jenkins and Docker integration

If you’re 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, Jenkins installations don’t need to be supplemented with a suite of build tools such as Maven, Gradle or Ant.

And environments that integrate Jenkins with Docker need not be bothered with the ongoing of maintenance and upkeep of 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 and Jenkins Docker pipeline plugins
  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:

Your intro to GitHub Actions training course

Here’s how to get started with GitHub Actions:

Follow these tutorials to learn GitHub Actions fast.

  • sudo apt install docker.io
  • sudo usermod -a -G docker jenkins
  • reboot

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

With Jenkins and Docker integrated together, you can now build your applications using a combination of the two.

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 filesystem so they persist after the container is taken offline. That way and JAR, WAR or EAR files can be pushed to Artifactory or deployed to Tomcat for further testing or for production.

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

SearchAppArchitecture

SearchSoftwareQuality

SearchCloudComputing

SearchSecurity

SearchAWS

Close