Docker in Jenkins Pipelines

30 minutes
  • 3 Learning Objectives

About this Hands-on Lab

Jenkins as a CI/CD tool offers way more than just being able to build and compile code. It can integrate and utilize a host of other technologies, one of which is Docker, a tool for containerizing your workloads.

In this hands-on lab we will be creating a Jenkins Pipeline job which will create and deploy Docker containers with an application.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Jenkins UI

Once you access landing page for Jenkins, click New Item on the left side of the screen to proceed towards selecting the type of Jenkins job you want to create.

Once the new page loads, click on Pipeline and on the input box give a name to your job, such as MyDeclarativePipeline.

After selecting the type of job, Pipeline, and giving it a name, click OK to proceed with configuring the job.

Configuring the Jenkins Pipeline Job (Docker in Jenkins)

On the new page which loads for configuring the pipeline job, scroll down to the Pipeline section and ensure that Pipeline script is selected in the Definition drop-down. An input area should be visible. This could also be done using a Scripted Pipeline, however we will be using a Declarative Pipeline in this lab.
Copy the following body of text (which is basically in Groovy DSL format, a format understood by Jenkins) into the input area:

pipeline{
    agent {
        docker {
            image 'python:alpine'
            args '-u root'
        }
    }
    stages{
        stage('Prepare environment'){
            steps{
                sh 'apk add git'
            }
        }
        stage('Clone code from Git repository and setup python env'){
            steps{
                sh 'git clone https://github.com/linuxacademy/content-pipelines-cje-labs.git'
                sh 'pip install -r content-pipelines-cje-labs/lab3_lab4/dog_pics_downloader/requirements.txt'
            }
        }
        stage('test code'){
            steps{
                sh 'python content-pipelines-cje-labs/lab3_lab4/dog_pics_downloader/dog_pic_get_class.py'
            }
        }
    }
    post{
        always{
            echo "Job execution complete."
        }
        success{
            archiveArtifacts artifacts : '*.jpg'
        }
        unsuccessful{
            echo "Job execution status is failed, please check error logs"
        }
        cleanup{
                echo 'Cleaning up environment'
                sh 'rm -rf content-pipelines-cje-labs'
        }
    }
}

Leave everything else at the default settings and click on Save.

Note: This Declarative Pipeline will pull down and run a Docker container which has python3 and pip installed already.

Additionally, we are installing git inside the container to pull down the code we want to test.

Running/Building the Jenkins Pipeline Job

After hitting Save in the previous step, you will be taken to the control page for the job you just configured. There you can see the job status and other job statistics, including being able to edit the configuration again.

To kick off a build, click on Build Now. This should kick off the build and you should see the output of build status in a couple of places on your current screen.

  1. Under the Build History box to the left of your screen you’ll see a build number and a progress bar, if the build is currently running.
  2. The page will also be updated with a Stage View giving details of execution success (green) and time taken by each build stage that was defined in our Declarative Pipeline. Subsequent builds will add more data to average run times in the same area.

If you click on the build number under Build History, #1 for example, you will be taken to the details of the specific build. This will show things such as the Console Output of executed commands that were defined in the pipeline for each step.

On this page you’ll also find the build output files or Build Artifacts, which are the outcome of the pipeline stages that you built. In this case, if everything goes right, you should get all the downloaded .jpg files as artifacts that the execution/testing of the code produced.

Additional Resources

You are part of a Release Engineering team for a software company. Recently your developers have been working on a Python application that requires specific Python modules in order to run. Running the job within your Jenkins server needlessly bloats it with Python modules that the job requires for testing the code. Your team lead has asked you to containerize the whole build in a Docker container so that your Jenkins server does not get modified during these builds.

You have a rough idea about how to go forward with this:

  1. Run your Jenkins job inside a Docker container. The Docker image most suitable for this task is python:alpine. However, commands inside it need to be run as root, which can be accomplished by passing in the following arguments to the Docker agent/container: -u root
  2. You will then need to install git, as it's not provided by default with this image: apk add git
  3. Clone the source code and ancillary files from the git repo:
    • https://github.com/linuxacademy/content-pipelines-cje-labs.git
  4. Install Python modules required to run code: pip install -r content-pipelines-cje-labs/lab3_lab4/dog_pics_downloader/requirements.txt
    Note: Make sure you're providing the correct path to the Pipeline when executing this command.
  5. Execute/test code: python content-pipelines-cje-labs/lab3_lab4/dog_pics_downloader/dog_pic_get_class.py
    Note: Make sure you're providing the correct path to the Pipeline when executing this command.
  6. In the post section of Pipeline, ensure there's a success step which archives any .jpg extension files.

Using the information at hand, create a Jenkins Pipeline (Declarative) which accomplishes the tasks above.

Optionally, see if you can also include the following steps within post block:

  1. always: Echo out a friendly message about job execution completing.
  2. unsuccessful: Echo out a message about failure.
  3. cleanup: Clean up the workspace using the command: rm -rf content-pipelines-cje-labs
What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Get Started
Who’s going to be learning?

How many seats do you need?

  • $499 USD per seat per year
  • Billed Annually
  • Renews in 12 months

Ready to accelerate learning?

For over 25 licenses, a member of our sales team will walk you through a custom tailored solution for your business.


$2,495.00

Checkout
Sign In
Welcome Back!
Thanks for reaching out!

You’ll hear from us shortly. In the meantime, why not check out what our customers have to say about ACG?