How to run Jenkins builds on Docker for Continuous Integration 2021?

About Jenkins



Jenkins is an open-source automation tool written in Java with plugins built for Continuous Integration purposes. Jenkins is used to building and testing your software projects continuously making it easier for developers to integrate changes to the project, and making it easier for users to obtain a fresh build. It also allows you to continuously deliver your software by integrating with a large number of testing and deployment technologies.


How to run Jenkins builds on Docker for Continuous Integration 2021?


If your Jenkins builds don’t use Docker, you’re not playing the continuous integration game on the 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 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:


  • 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


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.

Also, read this article:

How to implement Facebook Pixel to tract data in 2021?



Comments

Popular posts from this blog

How to optimizations by Angular in 2021?

How to create a blog in PHP and MySQL database - DB design

Secure React SPA using Keycloak with OpenID Connect in 2021