Run a Docker container as a build agent via a Jenkins file

Share

Jenkins is an open-source CI/CD software that allows automated workflows and is essential to DevOps. This automated workflow is also called a Pipeline. These Pipelines test and report changes in the codebase. Moreover, they can be used to build software, automate the testing of those builds, prepare the codebase for deployment and/or delivery, and finally, deploy the codebase to the required platform.

As mentioned above, Jenkins can build files as a build agent. It allows the user to set up steps and instructions for an automated process. This Pipeline method then creates the files as instructed and performs the checks mentioned. An example of a build file is a Docker container. This Answer will discuss creating a Docker container as a build file using a Jenkins Pipeline.

Jenkins Pipeline for Docker
Jenkins Pipeline for Docker

Implementation

Before starting our Docker containers, we need to install Jenkins on our device. For this Answer, we will use Linux as our operating system. If you have another operating system, head to the official installation page for Jenkins and download it according to your requirements.

Install Jenkins

To begin, we need to open our terminal and enter the commands given below.

  1. We now need to execute these commands in our terminal one by one.

sudo apt install openjdk-8-jdk // installs Java Development Kit based on the Java SE 8 specification
sudo apt install git // not mandatory but recommended
sudo apt install maven // installs maven build management tool
wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add -
sudo sh -c 'echo deb https://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'
sudo apt update
sudo apt install jenkins // install jenkins
sudo systemctl start jenkins // start
Installation commands
  1. After we have done so, we can go to our browser and enter the following address in our address bar to be redirected to our Jenkins administration page.

localhost:8080 // default Jenkins port
Jenkins port
  1. We will copy the address given on the page and paste it into our terminal with the prefix "sudo cat". This will give us our password.

Initial password
Initial password
  1. We will be redirected to another page after we enter the correct password. There, we select either option depending on our preferences. It is recommended to install suggested plugins.

Plugin installation
Plugin installation
  1. After installation, we are prompted to set up a profile. We can either do that or skip by signing in as admin. We then complete the forms by following the navigation. Now, we have set up our Jenkins at our port 8080.

Set up Docker containers

Let us now set up the Docker Pipeline. For this, we must head to our Jenkins page.

www.localhost:8080
Jenkins homepage
  1. Once there, click on "Manage Jenkins," then click on "Plugins". Now we arrive at the plugin page. From here, we go to "Available Plugins" and search for "Docker Pipeline". After selecting the desired plugin, click "Download now and install after restart".

Install Docker Pipeline plugin
Install Docker Pipeline plugin
  1. Now click on the restart option and log in again to your Jenkins.

Restart Jenkins
Restart Jenkins
  1. As your Docker Pipeline has finished installing, click "New Item", enter the project name, and select "Pipeline". Now click "Ok" to proceed.

  2. For uploading our Dockerfile, we need a GitHub or a Docker Hub account. In this answer, we will use a GitHub account. Therefore, we will head to our GitHub repository and upload a Dockerfile of our own choice.

--| Main Branch
--| Dockerfile
--| JenkinsFile (can be renamed)
File hierarchy in GitHub repository
FROM ubuntu:20.04
# Update packages and install curl
RUN apt-get update && \
apt-get install -y curl
# Install Node.js
RUN curl -fsSL https://deb.nodesource.com/setup_14.x | bash - && \
apt-get install -y nodejs
pipeline {
agent { dockerfile true } // Use Dockerfile as the agent for the pipeline
stages {
stage('Test') {
steps {
sh '''
curl --version // Check the version of curl
node --version // Check the version of Node.js
npm --version // Check the version of npm
'''
}
}
}
}
Example Jenkins File
  1. In the configuration section, we need to attach our Pipeline script. We can either add it using its inbuilt text editor, or we can add our GitHub link. After modifying the configurations, we can click save and proceed to the next page.

Configurations for Jenkins pipeline
1 of 3
  1. Inside our Pipeline page, we can click "Build Now" to run our agents. When we do that, we see a task running, as shown in the image below. We can click the number to go to the build details on this.

Build running
Build running
  1. Here we click "Console Output" to check our Pipeline status. We can read through the prompts and see our process run from here. The final line of the output will tell us if our files ran successfully or not.

Pipeline ran successfully
Pipeline ran successfully

Conclusion

We have now learned how to use Jenkins to spin up Docker containers and perform our tasks through an automated Jenkin build agent. This allows us to create an isolated system to test our packages and applications every time. Moreover, it allows for better portability with automation, which helps boost the speed at which we can build and test applications. In essence, Jenkins provides an essential utility by creating a faster, more efficient, and better building and testing experience.

Copyright ©2024 Educative, Inc. All rights reserved