Introduction

In this section, we will explore how to use Jenkins with Docker. Docker is a platform that allows you to automate the deployment of applications inside lightweight, portable containers. Integrating Jenkins with Docker can significantly enhance your CI/CD pipelines by providing a consistent and isolated environment for building, testing, and deploying applications.

Key Concepts

  1. Docker Basics:

    • Containers: Lightweight, standalone, and executable packages that include everything needed to run a piece of software.
    • Images: Read-only templates used to create containers.
    • Dockerfile: A text file that contains instructions to build a Docker image.
  2. Jenkins and Docker Integration:

    • Docker Plugin: A Jenkins plugin that allows Jenkins to interact with Docker.
    • Docker Agents: Jenkins agents that run inside Docker containers.

Installing Docker

Before integrating Docker with Jenkins, you need to have Docker installed on your system. Follow the official Docker installation guide for your operating system: Docker Installation Guide.

Installing the Docker Plugin in Jenkins

  1. Access Jenkins Dashboard:

    • Open your Jenkins instance in a web browser.
    • Log in with your credentials.
  2. Navigate to Plugin Manager:

    • Go to Manage Jenkins > Manage Plugins.
  3. Install Docker Plugin:

    • In the Available tab, search for Docker.
    • Select Docker and Docker Pipeline plugins.
    • Click Install without restart.

Configuring Docker in Jenkins

  1. Add Docker Host:

    • Go to Manage Jenkins > Configure System.
    • Scroll down to Cloud section and click Add a new cloud > Docker.
    • Configure the Docker host URL (e.g., unix:///var/run/docker.sock for Unix-based systems).
  2. Test Connection:

    • Click Test Connection to ensure Jenkins can communicate with the Docker host.

Creating a Jenkins Job with Docker

Step-by-Step Example

  1. Create a New Job:

    • Go to Jenkins Dashboard.
    • Click New Item, enter a name, and select Pipeline.
  2. Configure Pipeline:

    • In the Pipeline section, select Pipeline script and enter the following script:
pipeline {
    agent {
        docker {
            image 'maven:3.6.3-jdk-8'
            args '-v /root/.m2:/root/.m2'
        }
    }
    stages {
        stage('Build') {
            steps {
                sh 'mvn --version'
            }
        }
    }
}

Explanation

  • agent: Specifies that the pipeline will run inside a Docker container.
  • docker: Defines the Docker configuration.
    • image: The Docker image to use (maven:3.6.3-jdk-8 in this case).
    • args: Additional arguments for the Docker run command (e.g., volume mounts).
  • stages: Defines the stages of the pipeline.
    • stage('Build'): A single stage named Build.
    • steps: The steps to execute in the stage.
      • sh 'mvn --version': Runs the mvn --version command inside the container to verify Maven installation.
  1. Save and Run:
    • Click Save and then Build Now to run the job.

Practical Exercise

Exercise: Build a Simple Java Application with Docker

  1. Objective: Create a Jenkins pipeline that builds a simple Java application using a Docker container.

  2. Steps:

    • Create a new Jenkins job of type Pipeline.
    • Use the following pipeline script:
pipeline {
    agent {
        docker {
            image 'openjdk:11'
            args '-v /root/.m2:/root/.m2'
        }
    }
    stages {
        stage('Clone Repository') {
            steps {
                git 'https://github.com/spring-projects/spring-petclinic.git'
            }
        }
        stage('Build') {
            steps {
                sh './mvnw clean package'
            }
        }
    }
}
  1. Explanation:

    • Clone Repository: Clones the Spring Petclinic repository from GitHub.
    • Build: Runs the Maven build command inside the Docker container.
  2. Run the Job:

    • Save the job and click Build Now.

Solution

  • Ensure Docker is installed and running.
  • Install the Docker plugin in Jenkins.
  • Configure the Docker host in Jenkins.
  • Create and run the Jenkins job with the provided pipeline script.

Conclusion

In this section, we covered the basics of using Jenkins with Docker. We installed the Docker plugin, configured Docker in Jenkins, and created a Jenkins job that runs inside a Docker container. By integrating Docker with Jenkins, you can create isolated and consistent build environments, making your CI/CD pipelines more robust and reliable.

Next, we will explore how to use Jenkins with Kubernetes in the following section.

© Copyright 2024. All rights reserved